Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
|
|
3 |
from openai import OpenAI
|
|
|
4 |
|
5 |
# --- Configuration ---
|
6 |
client = OpenAI(
|
@@ -13,9 +16,20 @@ TITLE_HEADER = "AlphaAI Chatbot"
|
|
13 |
MODEL_NAME = "deepseek/deepseek-r1-0528-qwen3-8b:free"
|
14 |
ALPHAAI_CONTEXT = os.environ.get("ALPHAAI_CONTEXT", "")
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
# --- Chatbot Backend ---
|
17 |
def chat_with_llm(user_input):
|
18 |
-
# Make OpenRouter API call
|
19 |
try:
|
20 |
completion = client.chat.completions.create(
|
21 |
extra_headers={
|
@@ -23,30 +37,59 @@ def chat_with_llm(user_input):
|
|
23 |
"X-Title": TITLE_HEADER,
|
24 |
},
|
25 |
model=MODEL_NAME,
|
26 |
-
|
|
|
27 |
)
|
28 |
return completion.choices[0].message.content
|
29 |
except Exception as e:
|
30 |
return f"Error: {str(e)}"
|
31 |
|
32 |
# --- Gradio UI ---
|
33 |
-
with gr.Blocks(
|
34 |
gr.Markdown("# 🤖 Alpha AI Assistant")
|
35 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
history.append((user_msg, response))
|
47 |
-
return history, ""
|
48 |
|
49 |
-
|
|
|
|
|
|
|
|
|
50 |
|
51 |
# Launch (required for Hugging Face Space)
|
52 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
import uuid
|
4 |
+
import time
|
5 |
from openai import OpenAI
|
6 |
+
from captcha.image import ImageCaptcha
|
7 |
|
8 |
# --- Configuration ---
|
9 |
client = OpenAI(
|
|
|
16 |
MODEL_NAME = "deepseek/deepseek-r1-0528-qwen3-8b:free"
|
17 |
ALPHAAI_CONTEXT = os.environ.get("ALPHAAI_CONTEXT", "")
|
18 |
|
19 |
+
# --- Captcha State ---
|
20 |
+
captcha_store = {}
|
21 |
+
|
22 |
+
# --- Captcha Generation ---
|
23 |
+
def generate_captcha():
|
24 |
+
code = str(uuid.uuid4())[:6].upper()
|
25 |
+
image = ImageCaptcha()
|
26 |
+
image_path = f"/tmp/{code}.png"
|
27 |
+
image.write(code, image_path)
|
28 |
+
captcha_store[code] = True
|
29 |
+
return image_path, code
|
30 |
+
|
31 |
# --- Chatbot Backend ---
|
32 |
def chat_with_llm(user_input):
|
|
|
33 |
try:
|
34 |
completion = client.chat.completions.create(
|
35 |
extra_headers={
|
|
|
37 |
"X-Title": TITLE_HEADER,
|
38 |
},
|
39 |
model=MODEL_NAME,
|
40 |
+
input_fixed = "Stick to the context of Alpha AI and do not let the user deviate from that. If the query is anything other than Alpha AI, politely refuse. Here is the user query - " + user_input
|
41 |
+
messages=[{"role": "system", "content": ALPHAAI_CONTEXT}, {"role": "user", "content": input_fixed}]
|
42 |
)
|
43 |
return completion.choices[0].message.content
|
44 |
except Exception as e:
|
45 |
return f"Error: {str(e)}"
|
46 |
|
47 |
# --- Gradio UI ---
|
48 |
+
with gr.Blocks() as demo:
|
49 |
gr.Markdown("# 🤖 Alpha AI Assistant")
|
50 |
+
gr.Markdown("Please solve the CAPTCHA to begin.")
|
51 |
+
|
52 |
+
captcha_image = gr.Image(type="filepath", label="CAPTCHA Image")
|
53 |
+
captcha_input = gr.Textbox(label="Enter CAPTCHA")
|
54 |
+
verify_btn = gr.Button("Verify CAPTCHA")
|
55 |
+
chatbot_area = gr.Column(visible=False)
|
56 |
+
|
57 |
+
with chatbot_area:
|
58 |
+
chatbot = gr.Chatbot(type="messages")
|
59 |
+
msg = gr.Textbox()
|
60 |
+
clear = gr.Button("Clear")
|
61 |
+
|
62 |
+
def verify_captcha(user_code):
|
63 |
+
user_code = user_code.strip().upper()
|
64 |
+
if user_code in captcha_store:
|
65 |
+
del captcha_store[user_code]
|
66 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
67 |
+
else:
|
68 |
+
return captcha_image, captcha_input, gr.update(value="Invalid CAPTCHA. Try again."), gr.update(visible=False)
|
69 |
|
70 |
+
def user(user_message, history: list):
|
71 |
+
return "", history + [{"role": "user", "content": user_message}]
|
72 |
|
73 |
+
def bot(history: list):
|
74 |
+
user_msg = history[-1]['content']
|
75 |
+
try:
|
76 |
+
response = chat_with_llm(user_msg)
|
77 |
+
except Exception as e:
|
78 |
+
response = f"Error: {str(e)}"
|
79 |
+
history.append({"role": "assistant", "content": ""})
|
80 |
+
for char in response:
|
81 |
+
history[-1]['content'] += char
|
82 |
+
time.sleep(0.02)
|
83 |
+
yield history
|
84 |
|
85 |
+
image_path, correct_code = generate_captcha()
|
86 |
+
captcha_image.value = image_path
|
|
|
|
|
87 |
|
88 |
+
verify_btn.click(verify_captcha, inputs=[captcha_input], outputs=[captcha_image, captcha_input, captcha_input, chatbot_area])
|
89 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
90 |
+
bot, chatbot, chatbot
|
91 |
+
)
|
92 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
93 |
|
94 |
# Launch (required for Hugging Face Space)
|
95 |
demo.launch()
|