K00B404 commited on
Commit
c4b7c58
·
verified ·
1 Parent(s): 9223106

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -7
app.py CHANGED
@@ -1,9 +1,7 @@
1
- import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
 
@@ -39,9 +37,7 @@ def respond(
39
  response += token
40
  yield response
41
 
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
  demo = gr.ChatInterface(
46
  respond,
47
  additional_inputs=[
@@ -59,5 +55,56 @@ demo = gr.ChatInterface(
59
  )
60
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  if __name__ == "__main__":
63
  demo.launch()
 
1
+ """import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+
 
 
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
 
7
 
 
37
  response += token
38
  yield response
39
 
40
+
 
 
41
  demo = gr.ChatInterface(
42
  respond,
43
  additional_inputs=[
 
55
  )
56
 
57
 
58
+ if __name__ == "__main__":
59
+ demo.launch()
60
+ """
61
+
62
+ import os
63
+ import gradio as gr
64
+ from huggingface_hub import InferenceClient
65
+
66
+ # Retrieve the API token from the environment variable
67
+ API_TOKEN = os.getenv("HF_READ_TOKEN")
68
+
69
+ # Initialize the Hugging Face Inference Client
70
+ client = InferenceClient(
71
+ "mistralai/Mistral-Nemo-Instruct-2407",
72
+ token=API_TOKEN
73
+ )
74
+
75
+ # System prompt to define model behavior
76
+ system_prompt = "You are a helpful assistant that provides concise and accurate answers."
77
+
78
+ # Function to handle the chat completion
79
+ def hf_chat(user_input):
80
+ messages = [
81
+ {"role": "system", "content": system_prompt},
82
+ {"role": "user", "content": user_input}
83
+ ]
84
+ response = ""
85
+ for message in client.chat_completion(
86
+ messages=messages,
87
+ max_tokens=500,
88
+ stream=True,
89
+ ):
90
+ response += message.choices[0].delta.content
91
+ return response
92
+
93
+ # Gradio interface
94
+ with gr.Blocks() as demo:
95
+ gr.Markdown("# Hugging Face Chat Completion")
96
+ with gr.Row():
97
+ with gr.Column():
98
+ user_input = gr.Textbox(
99
+ label="Enter your message",
100
+ placeholder="Ask me anything..."
101
+ )
102
+ submit_btn = gr.Button("Submit")
103
+ with gr.Column():
104
+ output = gr.Textbox(label="Response")
105
+
106
+ submit_btn.click(fn=hf_chat, inputs=user_input, outputs=output)
107
+
108
+ # Launch Gradio app
109
  if __name__ == "__main__":
110
  demo.launch()