Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,89 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
-
# Load the model and tokenizer
|
| 6 |
-
model_name = "Qwen/Qwen2.5-72B-Instruct"
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Define the Gradio interface
|
| 18 |
css = """
|
|
@@ -22,18 +93,6 @@ css = """
|
|
| 22 |
}
|
| 23 |
"""
|
| 24 |
|
| 25 |
-
iface = gr.Interface(
|
| 26 |
-
fn=chatbot,
|
| 27 |
-
inputs=gr.Textbox(label="You:"),
|
| 28 |
-
outputs=gr.Textbox(label="Chatbot:"),
|
| 29 |
-
title="Qwen Chatbot",
|
| 30 |
-
description="A chatbot using the Qwen/Qwen2.5-72B-Instruct model.",
|
| 31 |
-
css=css
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
# Launch the interface
|
| 35 |
-
iface.launch()
|
| 36 |
-
|
| 37 |
|
| 38 |
|
| 39 |
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
import random as rd
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
from huggingface_hub import InferenceClient
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def respond (message, history):
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
messages = [{"role": "system", "content": "You are a chatbot who is sassy and doesnt explain an anser unless someone asks twice."}]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
if history:
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
messages.extend(history)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
messages.append({"role": "user", "content": message})
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
response = ""
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
for message in client.chat_completion(
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
messages,
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
max_tokens=500,
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
temperature=0.2,
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
top_p=0.9,
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
stream=True
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
):
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
token = message.choices[0].delta.content
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
response += token
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
yield response
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
chatbot = gr.ChatInterface(respond, type = "messages")
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
chatbot.launch(debug=True)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
|
| 88 |
# Define the Gradio interface
|
| 89 |
css = """
|
|
|
|
| 93 |
}
|
| 94 |
"""
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
|
| 98 |
|