Shankarm08 commited on
Commit
0817137
·
verified ·
1 Parent(s): 25d98f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -2,16 +2,33 @@ import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
 
 
 
5
  client = InferenceClient(
6
- model="microsoft/Phi-3-mini-4k-instruct",
7
- token=os.getenv("HF_TOKEN")
8
  )
9
 
 
 
10
  def chatbot(message, history):
11
- messages = [{"role": "user", "content": message}]
12
- response = client.chat_completion(messages=messages, max_tokens=100)
 
 
 
 
 
 
 
 
 
 
 
 
13
  return response.choices[0].message.content
14
 
15
- demo = gr.ChatInterface(fn=chatbot)
16
 
17
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
 
5
+ # Read token from Hugging Face Space Secrets
6
+ HF_TOKEN = os.environ.get("HF_TOKEN")
7
+
8
  client = InferenceClient(
9
+ provider="hf-inference",
10
+ api_key=HF_TOKEN,
11
  )
12
 
13
+ SYSTEM_PROMPT = "You are a helpful assistant."
14
+
15
  def chatbot(message, history):
16
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
17
+
18
+ for user, assistant in history:
19
+ messages.append({"role": "user", "content": user})
20
+ messages.append({"role": "assistant", "content": assistant})
21
+
22
+ messages.append({"role": "user", "content": message})
23
+
24
+ response = client.chat.completions.create(
25
+ model="microsoft/Phi-3-mini-4k-instruct",
26
+ messages=messages,
27
+ max_tokens=100,
28
+ )
29
+
30
  return response.choices[0].message.content
31
 
32
+ demo = gr.ChatInterface(fn=chatbot, title="Simple HF Chatbot")
33
 
34
+ demo.launch()