Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,116 +1,90 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import json
|
|
|
6 |
from datetime import datetime
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
json.dump(history, f, indent=2, ensure_ascii=False)
|
|
|
30 |
|
31 |
-
|
32 |
-
|
|
|
|
|
33 |
|
34 |
-
|
|
|
|
|
35 |
try:
|
36 |
-
with open(
|
37 |
-
|
38 |
-
return
|
39 |
-
except:
|
40 |
-
return [], []
|
41 |
-
|
42 |
-
# LLM chat
|
43 |
-
def chat_with_groq(user_input, history):
|
44 |
-
try:
|
45 |
-
messages = [{"role": "system", "content": "You are Neobot, a helpful assistant."}]
|
46 |
-
messages += history + [{"role": "user", "content": user_input}]
|
47 |
-
res = requests.post(
|
48 |
-
"https://api.groq.com/openai/v1/chat/completions",
|
49 |
-
headers={"Authorization": f"Bearer {GROQ_KEY}", "Content-Type": "application/json"},
|
50 |
-
json={"model": "llama3-70b-8192", "messages": messages}
|
51 |
-
)
|
52 |
-
reply = res.json()["choices"][0]["message"]["content"]
|
53 |
-
reply = filter_emojis(reply)
|
54 |
-
history += [{"role": "user", "content": user_input}, {"role": "assistant", "content": reply}]
|
55 |
-
return "", history, history
|
56 |
except Exception as e:
|
57 |
-
|
58 |
-
history += [{"role": "assistant", "content": error}]
|
59 |
-
return "", history, history
|
60 |
|
61 |
-
#
|
62 |
-
def transcribe(audio_path):
|
63 |
-
if not audio_path: return ""
|
64 |
-
try:
|
65 |
-
temp_wav = f"{uuid.uuid4()}.wav"
|
66 |
-
AudioSegment.from_file(audio_path).export(temp_wav, format="wav")
|
67 |
-
result = MODEL.transcribe(temp_wav)
|
68 |
-
os.remove(temp_wav)
|
69 |
-
return result["text"]
|
70 |
-
except:
|
71 |
-
return "β Transcription failed"
|
72 |
-
|
73 |
-
# UI
|
74 |
with gr.Blocks(css="""
|
75 |
-
body {
|
76 |
-
.gr-
|
77 |
-
|
78 |
-
.
|
79 |
-
.scroll-sidebar button:hover { background: #eee; }
|
80 |
""") as demo:
|
81 |
|
82 |
state = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
with gr.Row():
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
btns = []
|
106 |
-
for f in list_saved_chats():
|
107 |
-
def load_fn(fname=f): return load_chat(fname)
|
108 |
-
btn = gr.Button(f[:-5])
|
109 |
-
btn.click(load_fn, outputs=[bot, state])
|
110 |
-
btns.append(btn)
|
111 |
-
return btns
|
112 |
-
|
113 |
-
refresh_btn.click(render_chat_buttons, outputs=[chat_list])
|
114 |
-
demo.load(render_chat_buttons, outputs=[chat_list])
|
115 |
-
|
116 |
-
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
import re
|
6 |
from datetime import datetime
|
7 |
+
|
8 |
+
# π Groq API setup
|
9 |
+
os.environ["OPENAI_API_KEY"] = "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" # Replace with your actual key
|
10 |
+
openai.api_key = os.environ["OPENAI_API_KEY"]
|
11 |
+
openai.api_base = "https://api.groq.com/openai/v1"
|
12 |
+
|
13 |
+
# π¬ Chat function
|
14 |
+
def chat_with_groq(message, history):
|
15 |
+
messages = [{"role": "system", "content": "You are JAWERIA'SBOT π€ β cheerful, emoji-savvy, and sleek."}]
|
16 |
+
messages += history
|
17 |
+
messages.append({"role": "user", "content": message})
|
18 |
+
response = openai.ChatCompletion.create(model="llama3-70b-8192", messages=messages)
|
19 |
+
reply = response["choices"][0]["message"]["content"]
|
20 |
+
history.append({"role": "user", "content": message})
|
21 |
+
history.append({"role": "assistant", "content": reply})
|
22 |
+
return "", history, history
|
23 |
+
|
24 |
+
# πΎ Save chat session with readable filename
|
25 |
+
def save_session(history):
|
26 |
+
prompt = next((m["content"] for m in history if m["role"] == "user"), "chat")
|
27 |
+
title = re.sub(r"[^\w\s]", "", prompt).strip()
|
28 |
+
title = " ".join(title.split()[:6])
|
29 |
+
timestamp = datetime.now().strftime("%b %d %Y %H-%M")
|
30 |
+
filename = f"{title} - {timestamp}.json"
|
31 |
+
with open(filename, "w", encoding="utf-8") as f:
|
32 |
json.dump(history, f, indent=2, ensure_ascii=False)
|
33 |
+
return f"β
Chat saved as: {filename[:-5]}"
|
34 |
|
35 |
+
# π List saved chats for dropdown
|
36 |
+
def list_saved_files():
|
37 |
+
files = [f[:-5] for f in os.listdir() if f.endswith(".json")]
|
38 |
+
return sorted(files)
|
39 |
|
40 |
+
# π₯ Load selected chat
|
41 |
+
def load_chat(name):
|
42 |
+
filename = f"{name}.json"
|
43 |
try:
|
44 |
+
with open(filename, "r", encoding="utf-8") as f:
|
45 |
+
history = json.load(f)
|
46 |
+
return history, history, f"β
Loaded {name}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
except Exception as e:
|
48 |
+
return [], [], f"β Could not load {name}: {e}"
|
|
|
|
|
49 |
|
50 |
+
# π Dark-themed UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
with gr.Blocks(css="""
|
52 |
+
body {background-color: #111 !important; color: white;}
|
53 |
+
.gr-chatbot-message {background-color: #222 !important; color: white !important; border-radius: 8px; padding: 6px;}
|
54 |
+
input[type='text'] {background-color: #222 !important; color: white !important; border: 1px solid #555;}
|
55 |
+
.gr-button {background-color: #007acc !important; color: white !important; border-radius: 6px;}
|
|
|
56 |
""") as demo:
|
57 |
|
58 |
state = gr.State([])
|
59 |
+
|
60 |
+
gr.Markdown("<h1 style='text-align:center; color:#00ccff;'>β¨ JAWERIA'SBOT π€</h1>")
|
61 |
+
gr.Markdown("<div style='text-align:center;'>Ask boldly, explore endlessly. JAWERIA'SBOT is always listening π</div>")
|
62 |
+
|
63 |
+
chatbot = gr.Chatbot(type="messages") # π No label shown
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
chat_input = gr.Textbox(placeholder="Type your message... π", show_label=False, scale=8)
|
67 |
+
send_btn = gr.Button("Send π", scale=2)
|
68 |
+
|
69 |
with gr.Row():
|
70 |
+
new_chat_btn = gr.Button("π Start New Chat")
|
71 |
+
save_btn = gr.Button("πΎ Save Chat")
|
72 |
+
|
73 |
+
save_msg = gr.Markdown()
|
74 |
+
load_msg = gr.Markdown()
|
75 |
+
|
76 |
+
saved_files_dropdown = gr.Dropdown(label="π Saved Chats", choices=list_saved_files(), interactive=True, allow_custom_value=False)
|
77 |
+
load_btn = gr.Button("π₯ Load Selected Chat")
|
78 |
+
|
79 |
+
# π Message interactions
|
80 |
+
send_btn.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
|
81 |
+
chat_input.submit(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
|
82 |
+
|
83 |
+
new_chat_btn.click(fn=lambda: ("", [], []), outputs=[chat_input, chatbot, state])
|
84 |
+
|
85 |
+
save_btn.click(fn=save_session, inputs=[state], outputs=[save_msg])
|
86 |
+
save_btn.click(fn=list_saved_files, outputs=[saved_files_dropdown]) # π Refresh dropdown after saving
|
87 |
+
|
88 |
+
load_btn.click(fn=load_chat, inputs=[saved_files_dropdown], outputs=[chatbot, state, load_msg])
|
89 |
+
|
90 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|