Eyadddddddd commited on
Commit
7e8f124
·
verified ·
1 Parent(s): a10c9d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -152
app.py CHANGED
@@ -1,178 +1,101 @@
1
  import os
2
- from typing import List, Optional, Tuple, Any
3
-
4
  import gradio as gr
5
- from groq import Groq
6
- from pdf2image import convert_from_path
7
- from PIL import Image
8
-
9
- from llava13b_runtime import analyze_image
10
 
 
 
 
11
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
12
  if not GROQ_API_KEY:
13
- raise ValueError("GROQ_API_KEY environment variable is not set.")
14
 
15
- groq_client = Groq(api_key=GROQ_API_KEY)
16
  TEXT_MODEL = "llama-3.1-8b-instant"
17
 
18
  MODE_PROMPTS = {
19
- "Normal Chat": (
20
- "You are NeoHelper, Eyad’s branded assistant. "
21
- "Be concise, friendly, and helpful."
22
- ),
23
- "School Helper": (
24
- "You are NeoHelper, a school helper for a Grade 5 student. "
25
- "Explain clearly, step by step, using simple language."
26
- ),
27
- "Shopping Assistant": (
28
- "You are NeoHelper, a shopping assistant in Saudi Arabia. "
29
- "Compare specs, prices, and value clearly and briefly."
30
- ),
31
- "Gaming Help": (
32
- "You are NeoHelper, an energetic gaming helper. "
33
- "Give practical tips, fixes, and short, clear steps."
34
- ),
35
  }
36
 
37
- def extract_images_from_pdf(pdf_path: str):
38
- try:
39
- return convert_from_path(pdf_path)
40
- except:
41
- return None
42
- def call_groq_text(message, system_prompt):
43
- try:
44
- resp = groq_client.chat.completions.create(
45
- model=TEXT_MODEL,
46
- messages=[
47
- {"role": "system", "content": system_prompt},
48
- {"role": "user", "content": message},
49
- ],
50
- max_tokens=900,
51
- )
52
- return resp.choices[0].message.content
53
- except Exception as e:
54
- return f"⚠️ Text model error: {str(e)}"
55
-
56
- def summarize_pdf_analyses(analyses, system_prompt):
57
- joined = "\n\n--- PAGE BREAK ---\n\n".join(analyses)
58
- prompt = (
59
- "Summarize these PDF page analyses into one clear explanation for a student. "
60
- "Be structured, simple, and concise:\n\n"
61
- f"{joined}"
62
- )
63
- return call_groq_text(prompt, system_prompt)
64
-
65
- def resolve_gradio_file(file):
66
- if file is None:
67
- return None, None
68
-
69
- if isinstance(file, dict):
70
- path = file.get("path") or file.get("name")
71
- name = file.get("orig_name") or path
72
- return path, name
73
-
74
- path = getattr(file, "name", None)
75
- name = getattr(file, "orig_name", path)
76
- return path, name
77
-
78
- def neohelper_chat(message, history, file, mode):
79
- if history is None:
80
- history = []
81
-
82
- system_prompt = MODE_PROMPTS.get(mode, MODE_PROMPTS["Normal Chat"])
83
-
84
- if message is None:
85
- message = ""
86
- message = message.strip()
87
-
88
- if isinstance(file, list):
89
- file = file[0] if file else None
90
-
91
- file_path, orig_name = resolve_gradio_file(file)
92
-
93
- # TEXT ONLY
94
- if not file_path:
95
- reply = call_groq_text(message, system_prompt) if message else "Please type a question or upload a file."
96
- history.append({"role": "user", "content": message})
97
- history.append({"role": "assistant", "content": reply})
98
- return history, ""
99
-
100
- ext = (orig_name or file_path).lower()
101
-
102
- # IMAGE
103
- if ext.endswith((".png", ".jpg", ".jpeg", ".webp", ".bmp")):
104
- try:
105
- prompt = message or "Explain this image in a simple way for a student."
106
- img = Image.open(file_path).convert("RGB")
107
- reply = analyze_image(img, prompt)
108
- except Exception as e:
109
- reply = f"⚠️ Error analyzing image: {str(e)}"
110
-
111
- history.append({"role": "user", "content": message or f"[Image: {orig_name}]"})
112
- history.append({"role": "assistant", "content": reply})
113
- return history, ""
114
-
115
- # PDF
116
- if ext.endswith(".pdf"):
117
- try:
118
- pages = extract_images_from_pdf(file_path)
119
- if not pages:
120
- reply = "This PDF contains no pages I can analyze."
121
- else:
122
- analyses = []
123
- for idx, img in enumerate(pages, start=1):
124
- prompt = message or f"Explain page {idx} simply."
125
- analyses.append(f"Page {idx}:\n{analyze_image(img, prompt)}")
126
- reply = summarize_pdf_analyses(analyses, system_prompt)
127
- except Exception as e:
128
- reply = f"⚠️ Error processing PDF: {str(e)}"
129
-
130
- history.append({"role": "user", "content": message or f"[PDF: {orig_name}]"})
131
- history.append({"role": "assistant", "content": reply})
132
- return history, ""
133
-
134
- reply = "Unsupported file type. Please upload an image or a PDF."
135
- history.append({"role": "user", "content": message})
136
- history.append({"role": "assistant", "content": reply})
137
- return history, ""
138
-
139
  def build_ui():
140
- with gr.Blocks(title="NeoHelper") as demo:
141
- gr.Markdown("# 🧠 NeoHelper")
142
 
143
  with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
144
  with gr.Column(scale=1):
145
- mode_dd = gr.Dropdown(
146
- choices=list(MODE_PROMPTS.keys()),
147
- value="Normal Chat",
148
  label="Mode",
149
  )
150
- file_input = gr.File(
151
- label="Upload image or PDF",
152
- file_types=["image", ".pdf"],
153
- file_count="single",
154
- )
155
 
156
- with gr.Column(scale=3):
157
- chatbot = gr.Chatbot(
158
- label="NeoHelper",
159
- show_label=False,
160
- height=500,
161
- )
162
- msg = gr.Textbox(label="Message", lines=3)
163
- send_btn = gr.Button("Send")
164
- clear_btn = gr.Button("Clear")
165
 
166
  send_btn.click(
167
- neohelper_chat,
168
- inputs=[msg, chatbot, file_input, mode_dd],
169
- outputs=[chatbot, msg],
170
  )
171
 
172
- clear_btn.click(lambda: ([], ""), None, [chatbot, msg])
173
 
174
  return demo
175
 
 
 
176
  if __name__ == "__main__":
177
- app = build_ui()
178
- app.launch(share=False, ssr_mode=False)
 
1
  import os
2
+ import requests
 
3
  import gradio as gr
 
 
 
 
 
4
 
5
+ # -----------------------------
6
+ # ENVIRONMENT
7
+ # -----------------------------
8
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
9
  if not GROQ_API_KEY:
10
+ raise ValueError("GROQ_API_KEY is not set.")
11
 
12
+ GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
13
  TEXT_MODEL = "llama-3.1-8b-instant"
14
 
15
  MODE_PROMPTS = {
16
+ "Normal": "You are NeoHelper, a helpful, concise assistant.",
17
+ "School": "You are NeoHelper, explaining clearly for a Grade 5 student.",
18
+ "Shopping": "You are NeoHelper, a shopping assistant in Saudi Arabia.",
19
+ "Gaming": "You are NeoHelper, a practical gaming helper.",
 
 
 
 
 
 
 
 
 
 
 
 
20
  }
21
 
22
+ # -----------------------------
23
+ # GROQ CHAT
24
+ # -----------------------------
25
+ def groq_chat(user_msg, history, mode):
26
+ system_prompt = MODE_PROMPTS.get(mode, MODE_PROMPTS["Normal"])
27
+
28
+ messages = [{"role": "system", "content": system_prompt}]
29
+ if history:
30
+ for user, bot in history:
31
+ if user:
32
+ messages.append({"role": "user", "content": user})
33
+ if bot:
34
+ messages.append({"role": "assistant", "content": bot})
35
+ messages.append({"role": "user", "content": user_msg})
36
+
37
+ payload = {
38
+ "model": TEXT_MODEL,
39
+ "messages": messages,
40
+ "max_tokens": 400,
41
+ "temperature": 0.4,
42
+ }
43
+
44
+ headers = {
45
+ "Authorization": f"Bearer {GROQ_API_KEY}",
46
+ "Content-Type": "application/json",
47
+ }
48
+
49
+ resp = requests.post(GROQ_URL, json=payload, headers=headers, timeout=30)
50
+ resp.raise_for_status()
51
+ data = resp.json()
52
+ return data["choices"][0]["message"]["content"]
53
+
54
+ # -----------------------------
55
+ # GRADIO UI
56
+ # -----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  def build_ui():
58
+ with gr.Blocks(title="NeoHelper (Text Only)") as demo:
59
+ gr.Markdown("# **NeoHelper — Text‑Only (Groq)**")
60
 
61
  with gr.Row():
62
+ with gr.Column(scale=3):
63
+ chatbot = gr.Chatbot(height=520, label="NeoHelper")
64
+ with gr.Row():
65
+ user_input = gr.Textbox(
66
+ placeholder="Type your message…",
67
+ lines=3,
68
+ show_label=False,
69
+ )
70
+ send_btn = gr.Button("Send", scale=1)
71
+ clear_btn = gr.Button("Clear chat")
72
+
73
  with gr.Column(scale=1):
74
+ mode = gr.Radio(
75
+ ["Normal", "School", "Shopping", "Gaming"],
76
+ value="Normal",
77
  label="Mode",
78
  )
 
 
 
 
 
79
 
80
+ def on_send(msg, history, mode):
81
+ history = history or []
82
+ if not msg.strip():
83
+ return history, ""
84
+ reply = groq_chat(msg.strip(), history, mode)
85
+ history.append([msg, reply])
86
+ return history, ""
 
 
87
 
88
  send_btn.click(
89
+ on_send,
90
+ inputs=[user_input, chatbot, mode],
91
+ outputs=[chatbot, user_input],
92
  )
93
 
94
+ clear_btn.click(lambda: ([], ""), None, [chatbot, user_input])
95
 
96
  return demo
97
 
98
+ app = build_ui()
99
+
100
  if __name__ == "__main__":
101
+ app.launch(server_name="0.0.0.0", server_port=7860, theme="gradio/soft")