Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# Initialize client with Hugging Face OpenAI-compatible endpoint
|
| 6 |
+
client = OpenAI(
|
| 7 |
+
base_url="https://router.huggingface.co/v1",
|
| 8 |
+
api_key=os.environ["HF_TOKEN"], # Hugging Face token stored in Secrets
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# Function to handle chat conversation
|
| 12 |
+
def chat_with_deepseek(message, history):
|
| 13 |
+
try:
|
| 14 |
+
# Convert Gradio history into OpenAI format
|
| 15 |
+
messages = []
|
| 16 |
+
for user_msg, bot_msg in history:
|
| 17 |
+
messages.append({"role": "user", "content": user_msg})
|
| 18 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 19 |
+
messages.append({"role": "user", "content": message})
|
| 20 |
+
|
| 21 |
+
completion = client.chat.completions.create(
|
| 22 |
+
model="deepseek-ai/DeepSeek-V3.1:fireworks-ai",
|
| 23 |
+
messages=messages,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
reply = completion.choices[0].message.content
|
| 27 |
+
return reply
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return f"⚠️ Error: {str(e)}"
|
| 30 |
+
|
| 31 |
+
# Gradio Chat UI
|
| 32 |
+
with gr.Blocks(theme="soft", css="""
|
| 33 |
+
#chatbot {
|
| 34 |
+
border: 2px solid #8e44ad !important;
|
| 35 |
+
border-radius: 12px !important;
|
| 36 |
+
padding: 10px;
|
| 37 |
+
}
|
| 38 |
+
#inputbox textarea {
|
| 39 |
+
border: 2px solid #8e44ad !important;
|
| 40 |
+
border-radius: 8px !important;
|
| 41 |
+
padding: 8px;
|
| 42 |
+
font-size: 15px;
|
| 43 |
+
}
|
| 44 |
+
#submitbtn {
|
| 45 |
+
border: 2px solid #8e44ad !important;
|
| 46 |
+
border-radius: 8px !important;
|
| 47 |
+
background: #8e44ad !important;
|
| 48 |
+
color: white !important;
|
| 49 |
+
font-weight: bold !important;
|
| 50 |
+
padding: 10px 20px;
|
| 51 |
+
}
|
| 52 |
+
#submitbtn:hover {
|
| 53 |
+
background: #732d91 !important;
|
| 54 |
+
border-color: #732d91 !important;
|
| 55 |
+
}
|
| 56 |
+
""") as demo:
|
| 57 |
+
gr.HTML(
|
| 58 |
+
"""
|
| 59 |
+
<div style="text-align: center; padding: 15px;">
|
| 60 |
+
<h1 style="color: #2C3E50; margin-bottom: 5px;">🤖 AI Text Assistant</h1>
|
| 61 |
+
<h2 style="color: #8e44ad; margin-top: 0;">Powered by DeepSeek 3.1</h2>
|
| 62 |
+
<p style="font-size: 16px; color: #555;">
|
| 63 |
+
Have a conversation with DeepSeek — your AI-powered assistant.
|
| 64 |
+
</p>
|
| 65 |
+
</div>
|
| 66 |
+
"""
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
chatbot = gr.Chatbot(label="💬 Conversation", elem_id="chatbot")
|
| 70 |
+
user_input = gr.Textbox(
|
| 71 |
+
label="✍️ Your Message",
|
| 72 |
+
placeholder="Type a message and press Enter...",
|
| 73 |
+
lines=2,
|
| 74 |
+
elem_id="inputbox"
|
| 75 |
+
)
|
| 76 |
+
submit_btn = gr.Button("Send", elem_id="submitbtn")
|
| 77 |
+
|
| 78 |
+
# Event: when user submits input
|
| 79 |
+
submit_btn.click(
|
| 80 |
+
fn=chat_with_deepseek,
|
| 81 |
+
inputs=[user_input, chatbot],
|
| 82 |
+
outputs=chatbot
|
| 83 |
+
)
|
| 84 |
+
user_input.submit(
|
| 85 |
+
fn=chat_with_deepseek,
|
| 86 |
+
inputs=[user_input, chatbot],
|
| 87 |
+
outputs=chatbot
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
demo.launch()
|