tushar310 commited on
Commit
cd94be8
·
verified ·
1 Parent(s): 6fb6791

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -55
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- import random
3
  import os
4
  from openai import OpenAI
5
 
@@ -11,32 +10,11 @@ client = OpenAI(
11
 
12
  REFERER_HEADER = "https://alphaai.org.in"
13
  TITLE_HEADER = "AlphaAI Chatbot"
14
- MODEL_NAME = "google/gemini-2.0-flash-exp:free"
15
  ALPHAAI_CONTEXT = os.environ.get("ALPHAAI_CONTEXT", "")
16
 
17
- # --- CAPTCHA Logic ---
18
- def generate_captcha():
19
- a, b = random.randint(1, 9), random.randint(1, 9)
20
- return f"What is {a} + {b}?", a + b
21
-
22
- # Store correct answers in-memory (for stateless apps, better use secure session)
23
- captcha_answers = {}
24
-
25
  # --- Chatbot Backend ---
26
- def chat_with_llm(user_input, session_id, captcha_input):
27
- correct_answer = captcha_answers.get(session_id)
28
- if correct_answer is None:
29
- return "CAPTCHA not initialized properly. Please reload.", generate_new_captcha(session_id)[0]
30
-
31
- try:
32
- if int(captcha_input) != correct_answer:
33
- return "Incorrect CAPTCHA. Try again.", generate_new_captcha(session_id)[0]
34
- except ValueError:
35
- return "Please enter a valid number for the CAPTCHA.", generate_new_captcha(session_id)[0]
36
-
37
- # Prepare prompt with context
38
- full_prompt = f"You are Alpha AI's assistant. Use the following context to answer user questions.\n\n{ALPHAAI_CONTEXT}\n\nUser: {user_input}"
39
-
40
  # Make OpenRouter API call
41
  try:
42
  completion = client.chat.completions.create(
@@ -47,50 +25,28 @@ def chat_with_llm(user_input, session_id, captcha_input):
47
  model=MODEL_NAME,
48
  messages=[{"role": "system", "content": ALPHAAI_CONTEXT}, {"role": "user", "content": user_input}]
49
  )
50
- response = completion.choices[0].message.content
51
- return response, generate_new_captcha(session_id)[0]
52
  except Exception as e:
53
- return f"Error: {str(e)}", generate_new_captcha(session_id)[0]
54
-
55
-
56
- def generate_new_captcha(session_id):
57
- captcha_q, captcha_ans = generate_captcha()
58
- captcha_answers[session_id] = captcha_ans
59
- return captcha_q, captcha_ans
60
-
61
 
62
  # --- Gradio UI ---
63
- def start_session():
64
- session_id = str(random.randint(100000, 999999))
65
- captcha_q, _ = generate_new_captcha(session_id)
66
- return session_id, captcha_q
67
-
68
  with gr.Blocks(theme=gr.themes.Base(primary_hue="blue", secondary_hue="gray", font=["Inter", "sans-serif"])) as demo:
69
- gr.Markdown("# 🤖 Alpha AI Assistant with CAPTCHA")
70
- gr.Markdown("Welcome to Alpha AI’s intelligent assistant. Please solve the CAPTCHA to continue.")
71
 
72
  with gr.Row():
73
  chatbot = gr.Chatbot()
74
 
75
  with gr.Row():
76
  user_input = gr.Textbox(label="Your Message")
77
- captcha_input = gr.Textbox(label="CAPTCHA Answer")
78
-
79
- with gr.Row():
80
- session_id = gr.Textbox(visible=False)
81
- captcha_display = gr.Textbox(label="CAPTCHA", interactive=False)
82
  submit_btn = gr.Button("Submit")
83
 
84
- def handle_submit(user_msg, sid, cap_input):
85
- response, new_captcha_q = chat_with_llm(user_msg, sid, cap_input)
86
- return [(user_msg, response)], sid, new_captcha_q
87
-
88
- def init_session():
89
- sid, cap_q = start_session()
90
- return sid, cap_q
91
 
92
- submit_btn.click(handle_submit, inputs=[user_input, session_id, captcha_input], outputs=[chatbot, session_id, captcha_display])
93
- demo.load(init_session, inputs=[], outputs=[session_id, captcha_display])
94
 
95
  # Launch (required for Hugging Face Space)
96
  demo.launch()
 
1
  import gradio as gr
 
2
  import os
3
  from openai import OpenAI
4
 
 
10
 
11
  REFERER_HEADER = "https://alphaai.org.in"
12
  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(
 
25
  model=MODEL_NAME,
26
  messages=[{"role": "system", "content": ALPHAAI_CONTEXT}, {"role": "user", "content": user_input}]
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(theme=gr.themes.Base(primary_hue="blue", secondary_hue="gray", font=["Inter", "sans-serif"])) as demo:
34
+ gr.Markdown("# 🤖 Alpha AI Assistant")
35
+ gr.Markdown("Welcome to Alpha AI’s intelligent assistant. Please complete the CAPTCHA on the site before using.")
36
 
37
  with gr.Row():
38
  chatbot = gr.Chatbot()
39
 
40
  with gr.Row():
41
  user_input = gr.Textbox(label="Your Message")
 
 
 
 
 
42
  submit_btn = gr.Button("Submit")
43
 
44
+ def handle_submit(user_msg, history):
45
+ response = chat_with_llm(user_msg)
46
+ history.append((user_msg, response))
47
+ return history, ""
 
 
 
48
 
49
+ submit_btn.click(handle_submit, inputs=[user_input, chatbot], outputs=[chatbot, user_input])
 
50
 
51
  # Launch (required for Hugging Face Space)
52
  demo.launch()