Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
html_response = """
|
| 30 |
-
<div id="typing-output" style="font-family: Arial, sans-serif; font-size: 16px; white-space: pre-wrap;"></div>
|
| 31 |
-
<script>
|
| 32 |
-
const text = '""" + escaped_response + """';
|
| 33 |
-
let index = 0;
|
| 34 |
-
const speed = 30; // Typing speed in milliseconds
|
| 35 |
-
const outputElement = document.getElementById('typing-output');
|
| 36 |
-
|
| 37 |
-
function typeWriter() {
|
| 38 |
-
if (index < text.length) {
|
| 39 |
-
outputElement.innerHTML += text.charAt(index);
|
| 40 |
-
index++;
|
| 41 |
-
setTimeout(typeWriter, speed);
|
| 42 |
-
}
|
| 43 |
-
}
|
| 44 |
-
typeWriter();
|
| 45 |
-
</script>
|
| 46 |
-
"""
|
| 47 |
-
return html_response
|
| 48 |
|
| 49 |
-
# Create Gradio interface
|
| 50 |
-
demo = gr.
|
| 51 |
-
fn=
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
css="""
|
| 57 |
-
body { background-color: #f0f2f5; }
|
| 58 |
-
.gr-box { border-radius: 8px; padding: 20px; }
|
| 59 |
-
#typing-output { background: #fff; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
|
| 60 |
-
"""
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
if __name__ == "__main__":
|
| 65 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from threading import Thread
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Load non-gated model and tokenizer
|
| 7 |
+
model_id = "Qwen/Qwen2-7B-Instruct"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
|
| 10 |
|
| 11 |
+
def chat(message, history):
|
| 12 |
+
# Build message history
|
| 13 |
+
messages = [
|
| 14 |
+
{"role": "system", "content": "You are a helpful and friendly assistant."}
|
| 15 |
+
] + history + [{"role": "user", "content": message}]
|
| 16 |
|
| 17 |
+
# Prepare inputs
|
| 18 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 19 |
+
|
| 20 |
+
# Set up streamer for live typing
|
| 21 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 22 |
+
|
| 23 |
+
# Generation kwargs
|
| 24 |
+
generation_kwargs = {
|
| 25 |
+
"inputs": inputs,
|
| 26 |
+
"streamer": streamer,
|
| 27 |
+
"max_new_tokens": 256,
|
| 28 |
+
"do_sample": True,
|
| 29 |
+
"top_p": 0.95,
|
| 30 |
+
"temperature": 0.7,
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# Run generation in a separate thread
|
| 34 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 35 |
+
thread.start()
|
| 36 |
|
| 37 |
+
# Yield tokens for live streaming
|
| 38 |
+
generated_text = ""
|
| 39 |
+
for new_text in streamer:
|
| 40 |
+
generated_text += new_text
|
| 41 |
+
yield generated_text
|
| 42 |
|
| 43 |
+
thread.join()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Create Gradio chat interface
|
| 46 |
+
demo = gr.ChatInterface(
|
| 47 |
+
fn=chat,
|
| 48 |
+
type="messages",
|
| 49 |
+
title="Qwen2-7B Chatbot",
|
| 50 |
+
description="Chat with a non-gated Qwen2-7B-Instruct model. Responses stream live.",
|
| 51 |
+
examples=[["Tell me a fun fact."], ["Explain neural networks in simple terms."]],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
+
demo.launch()
|
|
|
|
|
|