traversaal-ai commited on
Commit
77bfe0b
1 Parent(s): 418756d

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +30 -34
run.py CHANGED
@@ -8,39 +8,35 @@ endpoint_url = "https://ko60a2m26ylqgri6.us-east-1.aws.endpoints.huggingface.clo
8
  # Streaming Client
9
  client = InferenceClient(endpoint_url, token=hf_token)
10
 
11
- # generation parameter
12
- gen_kwargs = dict(
13
- max_new_tokens=1024,
14
- top_k=30,
15
- top_p=0.9,
16
- temperature=0.2,
17
- repetition_penalty=1.05, #1.02
18
- stop=["\nUser:", "<|endoftext|>", "</s>"],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  )
20
- # prompt
21
- # prompt = "What can you do in Nuremberg, Germany? Give me 3 Tips"
22
 
23
-
24
-
25
- with gr.Blocks() as demo:
26
- chatbot = gr.Chatbot()
27
- msg = gr.Textbox()
28
- clear = gr.Button("Clear")
29
-
30
- def user(user_message, history):
31
- return "", history + [[user_message, None]]
32
-
33
- def bot(history):
34
- bot_message = client.text_generation(history, stream=True, details=True, **gen_kwargs)
35
- history[-1][1] = ""
36
- for character in bot_message:
37
- history[-1][1] += character
38
- yield history
39
-
40
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
41
- bot, chatbot, chatbot
42
- )
43
- clear.click(lambda: None, None, chatbot, queue=False)
44
-
45
- if __name__ == "__main__":
46
- demo.launch()
 
8
  # Streaming Client
9
  client = InferenceClient(endpoint_url, token=hf_token)
10
 
11
+ def generate_text(prompt):
12
+ """Generates text using the Hugging Face Inference API."""
13
+ chat_prompt = f"""
14
+
15
+ ### Instruction:
16
+ You are a chatbot. Chat in Urdu
17
+
18
+ ### Input:
19
+ {prompt}
20
+
21
+ ### Response:
22
+ ""
23
+ """
24
+ stream = client.text_generation(chat_prompt, stream=True, details=True, **gen_kwargs)
25
+ generated_text = ""
26
+ for r in stream:
27
+ if r.token.special:
28
+ continue
29
+ if r.token.text in gen_kwargs["stop"]:
30
+ break
31
+ generated_text += r.token.text
32
+ yield generated_text
33
+
34
+ iface = gr.Interface(
35
+ fn=generate_text,
36
+ inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
37
+ outputs="text",
38
+ title="Urdu Chatbot",
39
+ description="Ask me anything in Urdu!",
40
  )
 
 
41
 
42
+ iface.launch()