Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,110 +1,80 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
):
|
19 |
-
"""
|
20 |
-
Handles chat responses with error handling and validation
|
21 |
-
"""
|
22 |
-
try:
|
23 |
-
# Validate system message
|
24 |
-
if not system_message.strip():
|
25 |
-
system_message = "You are a helpful AI assistant."
|
26 |
-
|
27 |
-
# Build message history
|
28 |
-
messages = [{"role": "system", "content": system_message}]
|
29 |
-
|
30 |
-
for user_msg, assistant_msg in history:
|
31 |
-
if user_msg:
|
32 |
-
messages.append({"role": "user", "content": user_msg})
|
33 |
-
if assistant_msg:
|
34 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
35 |
-
|
36 |
-
messages.append({"role": "user", "content": message})
|
37 |
-
|
38 |
-
response = ""
|
39 |
-
|
40 |
-
# Stream the response
|
41 |
-
for chunk in client.chat_completion(
|
42 |
-
messages,
|
43 |
-
max_tokens=max_tokens,
|
44 |
-
stream=True,
|
45 |
-
temperature=temperature,
|
46 |
-
top_p=top_p,
|
47 |
-
):
|
48 |
-
if chunk.choices and chunk.choices[0].delta.content:
|
49 |
-
token = chunk.choices[0].delta.content
|
50 |
-
response += token
|
51 |
-
yield response
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
# Custom CSS for
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
65 |
"""
|
66 |
|
67 |
-
#
|
68 |
-
with gr.Blocks(css=
|
69 |
-
gr.Markdown("# 🚀
|
70 |
-
gr.Markdown("
|
71 |
|
72 |
-
|
73 |
-
system_input = gr.Textbox(
|
74 |
-
value="You are a helpful AI assistant.",
|
75 |
-
label="System Role",
|
76 |
-
info="Initial instructions for the AI"
|
77 |
-
)
|
78 |
-
max_tokens = gr.Slider(
|
79 |
-
minimum=32, maximum=2048, value=512,
|
80 |
-
step=32, label="Max Response Length"
|
81 |
-
)
|
82 |
-
temperature = gr.Slider(
|
83 |
-
minimum=0.1, maximum=2.0, value=0.7,
|
84 |
-
step=0.1, label="Creativity (Temperature)"
|
85 |
-
)
|
86 |
-
top_p = gr.Slider(
|
87 |
-
minimum=0.1, maximum=1.0, value=0.95,
|
88 |
-
step=0.05, label="Focus (Top-p)"
|
89 |
-
)
|
90 |
-
|
91 |
-
chat_interface = gr.ChatInterface(
|
92 |
respond,
|
93 |
-
additional_inputs=[
|
94 |
-
system_input,
|
95 |
-
max_tokens,
|
96 |
-
temperature,
|
97 |
-
top_p
|
98 |
-
],
|
99 |
examples=[
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
103 |
],
|
104 |
retry_btn=None,
|
105 |
undo_btn=None,
|
106 |
-
clear_btn="
|
107 |
)
|
|
|
|
|
108 |
|
109 |
if __name__ == "__main__":
|
110 |
-
demo.launch(
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load your fine-tuned model and tokenizer
|
6 |
+
model = AutoModelForCausalLM.from_pretrained(
|
7 |
+
"hackergeek/gemma-finetuned",
|
8 |
+
torch_dtype=torch.float16,
|
9 |
+
device_map="auto"
|
10 |
+
)
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("hackergeek/gemma-finetuned")
|
12 |
+
tokenizer.pad_token = tokenizer.eos_token
|
13 |
|
14 |
+
def format_prompt(message, history):
|
15 |
+
"""Format the prompt with conversation history"""
|
16 |
+
system_prompt = "You are a knowledgeable space expert assistant. Answer questions about astronomy, space exploration, and related topics in a clear and engaging manner."
|
17 |
+
prompt = f"<system>{system_prompt}</system>\n"
|
18 |
+
|
19 |
+
for user_msg, bot_msg in history:
|
20 |
+
prompt += f"<user>{user_msg}</user>\n<assistant>{bot_msg}</assistant>\n"
|
21 |
+
|
22 |
+
prompt += f"<user>{message}</user>\n<assistant>"
|
23 |
+
return prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
def respond(message, history):
|
26 |
+
# Format the prompt with conversation history
|
27 |
+
full_prompt = format_prompt(message, history)
|
28 |
+
|
29 |
+
# Tokenize input
|
30 |
+
inputs = tokenizer(full_prompt, return_tensors="pt", add_special_tokens=False).to(model.device)
|
31 |
+
|
32 |
+
# Generate response
|
33 |
+
outputs = model.generate(
|
34 |
+
**inputs,
|
35 |
+
max_new_tokens=1024,
|
36 |
+
temperature=0.7,
|
37 |
+
top_p=0.9,
|
38 |
+
repetition_penalty=1.1,
|
39 |
+
do_sample=True
|
40 |
+
)
|
41 |
+
|
42 |
+
# Decode and extract only the new response
|
43 |
+
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
44 |
+
|
45 |
+
return response
|
46 |
|
47 |
+
# Custom CSS for space theme
|
48 |
+
space_css = """
|
49 |
+
.gradio-container {
|
50 |
+
background: linear-gradient(45deg, #000000, #1a1a2e);
|
51 |
+
color: white;
|
52 |
+
}
|
53 |
+
.chatbot {
|
54 |
+
background-color: rgba(0, 0, 0, 0.7) !important;
|
55 |
+
border: 1px solid #4a4a4a !important;
|
56 |
+
}
|
57 |
"""
|
58 |
|
59 |
+
# Create the interface
|
60 |
+
with gr.Blocks(css=space_css, theme=gr.themes.Default(primary_hue="blue", secondary_hue="purple")) as demo:
|
61 |
+
gr.Markdown("# 🚀 Space Explorer Chatbot 🌌")
|
62 |
+
gr.Markdown("Ask me anything about space! Planets, stars, galaxies, or space exploration!")
|
63 |
|
64 |
+
chatbot = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
respond,
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
examples=[
|
67 |
+
"Explain black holes in simple terms",
|
68 |
+
"What's the latest news about Mars exploration?",
|
69 |
+
"How do stars form?",
|
70 |
+
"Tell me about the James Webb Space Telescope"
|
71 |
],
|
72 |
retry_btn=None,
|
73 |
undo_btn=None,
|
74 |
+
clear_btn="Clear History",
|
75 |
)
|
76 |
+
|
77 |
+
chatbot.chatbot.height = 600
|
78 |
|
79 |
if __name__ == "__main__":
|
80 |
+
demo.launch(share=True)
|