Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,178 +1,101 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 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
|
| 14 |
|
| 15 |
-
|
| 16 |
TEXT_MODEL = "llama-3.1-8b-instant"
|
| 17 |
|
| 18 |
MODE_PROMPTS = {
|
| 19 |
-
"Normal
|
| 20 |
-
|
| 21 |
-
|
| 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 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
{"role": "
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 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("#
|
| 142 |
|
| 143 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
with gr.Column(scale=1):
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
value="Normal
|
| 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 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
send_btn = gr.Button("Send")
|
| 164 |
-
clear_btn = gr.Button("Clear")
|
| 165 |
|
| 166 |
send_btn.click(
|
| 167 |
-
|
| 168 |
-
inputs=[
|
| 169 |
-
outputs=[chatbot,
|
| 170 |
)
|
| 171 |
|
| 172 |
-
clear_btn.click(lambda: ([], ""), None, [chatbot,
|
| 173 |
|
| 174 |
return demo
|
| 175 |
|
|
|
|
|
|
|
| 176 |
if __name__ == "__main__":
|
| 177 |
-
app =
|
| 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")
|
|
|