Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# ====== Load Model ======
|
| 7 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 8 |
+
pipe = pipeline(
|
| 9 |
+
"text-generation",
|
| 10 |
+
model="rahul7star/Qwen0.5-3B-Gita",
|
| 11 |
+
device=device,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# ====== Chat Function ======
|
| 15 |
+
def chat_with_model(message, history):
|
| 16 |
+
"""
|
| 17 |
+
message: new user message
|
| 18 |
+
history: list of (user, bot) pairs
|
| 19 |
+
"""
|
| 20 |
+
# Build message history for the model
|
| 21 |
+
messages = [{"role": "system", "content": "You are a friendly assistant trained on Gita knowledge."}]
|
| 22 |
+
for user, bot in history:
|
| 23 |
+
messages.append({"role": "user", "content": user})
|
| 24 |
+
messages.append({"role": "assistant", "content": bot})
|
| 25 |
+
messages.append({"role": "user", "content": message})
|
| 26 |
+
|
| 27 |
+
# Generate reply
|
| 28 |
+
output = pipe(messages, max_new_tokens=200, do_sample=True, temperature=0.7)
|
| 29 |
+
reply = output[0]['generated_text'][-1]['content'] if isinstance(output[0]['generated_text'], list) else output[0]['generated_text']
|
| 30 |
+
|
| 31 |
+
# Update history
|
| 32 |
+
history.append((message, reply))
|
| 33 |
+
return "", history
|
| 34 |
+
|
| 35 |
+
# ====== Gradio Interface ======
|
| 36 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 37 |
+
gr.Markdown("## 💬 Qwen0.5-3B-Gita — Conversational Assistant")
|
| 38 |
+
|
| 39 |
+
chatbot = gr.Chatbot(height=500)
|
| 40 |
+
msg = gr.Textbox(placeholder="Ask about the Gita, life, or philosophy...", label="Your Message")
|
| 41 |
+
clear = gr.Button("Clear")
|
| 42 |
+
|
| 43 |
+
msg.submit(chat_with_model, [msg, chatbot], [msg, chatbot])
|
| 44 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 45 |
+
|
| 46 |
+
# ====== Launch ======
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|