Spaces:
Running
Running
Upload app (11).py
Browse files- app (11).py +66 -0
app (11).py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
client = InferenceClient("https://vulture-awake-probably.ngrok-free.app/v1/chat/completions")
|
4 |
+
|
5 |
+
FIXED_MAX_TOKENS = 1024
|
6 |
+
FIXED_TEMPERATURE = 1
|
7 |
+
FIXED_TOP_P = 0.95
|
8 |
+
|
9 |
+
def respond(message, history):
|
10 |
+
# --- Syntax Error was here ---
|
11 |
+
# Corrected: Initialize messages as an empty list.
|
12 |
+
# If you had a system message previously, you might want to add it back, e.g.:
|
13 |
+
# FIXED_SYSTEM_MESSAGE = "Your system message here"
|
14 |
+
# messages = [{"role": "system", "content": FIXED_SYSTEM_MESSAGE}]
|
15 |
+
messages = []
|
16 |
+
# --- End of correction ---
|
17 |
+
|
18 |
+
for user_message, ai_message in history:
|
19 |
+
if user_message:
|
20 |
+
messages.append({"role": "user", "content": user_message})
|
21 |
+
if ai_message:
|
22 |
+
messages.append({"role": "assistant", "content": ai_message})
|
23 |
+
|
24 |
+
messages.append({"role": "user", "content": message})
|
25 |
+
|
26 |
+
response = ""
|
27 |
+
|
28 |
+
try:
|
29 |
+
for chunk in client.chat.completions.create(
|
30 |
+
messages=messages,
|
31 |
+
max_tokens=FIXED_MAX_TOKENS,
|
32 |
+
stream=True,
|
33 |
+
temperature=FIXED_TEMPERATURE,
|
34 |
+
top_p=FIXED_TOP_P,
|
35 |
+
|
36 |
+
):
|
37 |
+
if chunk.choices[0].delta.content is not None:
|
38 |
+
token = chunk.choices[0].delta.content
|
39 |
+
response += token
|
40 |
+
yield response
|
41 |
+
except Exception as e:
|
42 |
+
yield f"An error occurred: {e}"
|
43 |
+
|
44 |
+
header_image_path = "https://cdn-uploads.huggingface.co/production/uploads/6540a02d1389943fef4d2640/j61iZTDaK9g0UW3aWGwWi.gif"
|
45 |
+
|
46 |
+
|
47 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
48 |
+
|
49 |
+
gr.Image(
|
50 |
+
value=header_image_path,
|
51 |
+
label="Chatbot Header",
|
52 |
+
show_label=False,
|
53 |
+
interactive=False,
|
54 |
+
height=100,
|
55 |
+
elem_id="chatbot-logo"
|
56 |
+
)
|
57 |
+
|
58 |
+
gr.ChatInterface(
|
59 |
+
respond,
|
60 |
+
chatbot=gr.Chatbot(
|
61 |
+
height=700
|
62 |
+
)
|
63 |
+
)
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
demo.launch(show_api=False, share=True)
|