Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
|
5 |
+
# Retrieve the API key from environment variables
|
6 |
+
api_key = os.getenv("API_KEY")
|
7 |
+
|
8 |
+
# Initialize the InferenceClient with your provider and the API key from the environment variable
|
9 |
+
client = InferenceClient(
|
10 |
+
provider="together",
|
11 |
+
api_key=api_key
|
12 |
+
)
|
13 |
+
|
14 |
+
def chatbot_response(user_input, chat_history):
|
15 |
+
"""
|
16 |
+
Sends the user's input to the inference client and appends the response to the conversation history.
|
17 |
+
"""
|
18 |
+
messages = [{"role": "user", "content": user_input}]
|
19 |
+
|
20 |
+
# Get the response from the Hugging Face model
|
21 |
+
completion = client.chat.completions.create(
|
22 |
+
model="deepseek-ai/DeepSeek-R1",
|
23 |
+
messages=messages,
|
24 |
+
max_tokens=500,
|
25 |
+
)
|
26 |
+
|
27 |
+
# Extract the model's response
|
28 |
+
bot_message = completion.choices[0].message
|
29 |
+
chat_history.append((user_input, bot_message))
|
30 |
+
|
31 |
+
# Return an empty string to clear the input textbox and the updated chat history
|
32 |
+
return "", chat_history
|
33 |
+
|
34 |
+
# Create the Gradio Blocks interface
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("# DeepSeek-R1")
|
37 |
+
|
38 |
+
chatbot = gr.Chatbot()
|
39 |
+
|
40 |
+
state = gr.State([])
|
41 |
+
|
42 |
+
with gr.Row():
|
43 |
+
txt = gr.Textbox(placeholder="Type your message here...", show_label=False)
|
44 |
+
send_btn = gr.Button("Send")
|
45 |
+
|
46 |
+
txt.submit(
|
47 |
+
chatbot_response,
|
48 |
+
inputs=[txt, state],
|
49 |
+
outputs=[txt, chatbot]
|
50 |
+
)
|
51 |
+
send_btn.click(
|
52 |
+
chatbot_response,
|
53 |
+
inputs=[txt, state],
|
54 |
+
outputs=[txt, chatbot]
|
55 |
+
)
|
56 |
+
|
57 |
+
# Launch the interface
|
58 |
+
demo.launch()
|