Spaces:
Sleeping
Sleeping
File size: 9,076 Bytes
dc59429 95de487 a082581 dc59429 95de487 a082581 95de487 5bb65cf 4393f6c dc59429 315f611 38afa62 315f611 a082581 315f611 95de487 5bb65cf 4393f6c 3dec7f4 4393f6c 95de487 4393f6c 95de487 4393f6c dc59429 95de487 a082581 95de487 a082581 3dec7f4 4393f6c 3dec7f4 4393f6c 3dec7f4 38afa62 3dec7f4 4393f6c 5bb65cf 3dec7f4 95de487 5bb65cf 95de487 7f4ab6d 95de487 3dec7f4 7f4ab6d 5bb65cf 3dec7f4 5bb65cf 95de487 3dec7f4 7f4ab6d 9944367 3dec7f4 5bb65cf 95de487 7f4ab6d 95de487 9944367 95de487 3dec7f4 95de487 3dec7f4 38afa62 95de487 3dec7f4 315f611 5bb65cf 3dec7f4 95de487 5bb65cf 9944367 5bb65cf 4393f6c 7f4ab6d 5bb65cf 7f4ab6d 5bb65cf f2cea12 7f4ab6d 5bb65cf 9944367 5bb65cf 7f4ab6d 5bb65cf 95de487 3dec7f4 7f4ab6d 3dec7f4 7f4ab6d 95de487 7f4ab6d 5bb65cf 95de487 7f4ab6d 95de487 7f4ab6d 5bb65cf 9944367 95de487 3dec7f4 7f4ab6d 3dec7f4 7f4ab6d 95de487 7f4ab6d 5bb65cf 95de487 3dec7f4 7f4ab6d 3dec7f4 7f4ab6d 95de487 7f4ab6d 5bb65cf 9944367 95de487 7f4ab6d 3dec7f4 7f4ab6d dc59429 5bb65cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | import os
import re
import requests
import gradio as gr
from huggingface_hub import InferenceClient
# ========= CONFIG =========
CHAT_MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
ROUTER_URL = "https://router.huggingface.co/v1/chat/completions"
ASR_MODEL_ID = "openai/whisper-large-v3"
DEFAULT_SYSTEM_PROMPT = "You are a helpful AI assistant."
MAX_FILE_CHARS = 40_000
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
raise RuntimeError("HF_TOKEN is missing. Add it in Space Settings β Secrets as HF_TOKEN.")
HEADERS = {
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "application/json",
}
asr_client = InferenceClient(api_key=HF_TOKEN)
# ========= HELPERS =========
def normalize_whitespace(s: str) -> str:
return re.sub(r"\s+", " ", (s or "")).strip()
def safe_read_text_file(path: str) -> str:
with open(path, "r", encoding="utf-8", errors="ignore") as f:
return f.read()
def extract_text_from_pdf(path: str) -> str:
try:
from pypdf import PdfReader
except Exception:
return "PDF support not installed. Add 'pypdf' to requirements.txt."
reader = PdfReader(path)
parts = []
for page in reader.pages[:50]:
parts.append(page.extract_text() or "")
return "\n".join(parts)
def extract_file_text(file_path: str) -> str:
ext = os.path.splitext(file_path)[1].lower()
if ext in [".txt", ".md", ".csv", ".json", ".py", ".js", ".html", ".css", ".log", ".yaml", ".yml"]:
return safe_read_text_file(file_path)
if ext == ".pdf":
return extract_text_from_pdf(file_path)
return ""
def call_llm(messages, temperature=0.7, max_tokens=400):
payload = {
"model": CHAT_MODEL_ID,
"messages": messages,
"temperature": float(temperature),
"max_tokens": int(max_tokens),
}
resp = requests.post(ROUTER_URL, headers=HEADERS, json=payload, timeout=120)
if resp.status_code != 200:
try:
err = resp.json()
except Exception:
err = resp.text
raise RuntimeError(f"HTTP {resp.status_code}: {err}")
data = resp.json()
return data["choices"][0]["message"]["content"]
def chat_to_llm_messages(chat_messages, system_prompt: str, file_context: str, user_text: str):
msgs = [{"role": "system", "content": (system_prompt.strip() or DEFAULT_SYSTEM_PROMPT)}]
if (file_context or "").strip():
msgs.append({
"role": "system",
"content": "User uploaded file content (use this as reference):\n" + file_context
})
for m in (chat_messages or []):
role = (m.get("role") or "").lower()
content = m.get("content") or ""
if role in ("user", "assistant") and content:
msgs.append({"role": role, "content": content})
msgs.append({"role": "user", "content": user_text})
return msgs
# ========= ACTIONS =========
def on_upload_file(chat_messages, file_obj, file_context):
if file_obj is None:
return chat_messages, file_context, "No file selected"
file_path = getattr(file_obj, "name", None) or str(file_obj)
filename = os.path.basename(file_path)
file_size = os.path.getsize(file_path) if os.path.exists(file_path) else 0
extracted = extract_file_text(file_path)
if extracted:
extracted = extracted[:MAX_FILE_CHARS]
file_context = f"[{filename}]\n{extracted}\n\n" + (file_context or "")
chat_messages = (chat_messages or []) + [
{"role": "user", "content": f"π Uploaded file: {filename}"},
{"role": "assistant", "content": f"β
**{filename}** loaded ({file_size:,} bytes). Ask me anything about this document!"},
]
file_info = f"β
Loaded: {filename} ({file_size:,} bytes)"
else:
chat_messages = (chat_messages or []) + [
{"role": "user", "content": f"π Uploaded file: {filename}"},
{"role": "assistant", "content": f"π **{filename}** received. I'll answer based on its content."},
]
file_info = f"π Uploaded: {filename}"
return chat_messages, file_context, file_info
def on_record_audio(audio_path):
if not audio_path:
return ""
try:
out = asr_client.automatic_speech_recognition(audio_path, model=ASR_MODEL_ID)
text = getattr(out, "text", "") if out is not None else ""
return normalize_whitespace(text)
except Exception as e:
return f"[Voice recognition error: {str(e)[:50]}]"
def on_send(chat_messages, user_text, system_prompt, temperature, max_tokens, file_context):
user_text = (user_text or "").strip()
if not user_text:
return chat_messages, ""
chat_messages = chat_messages or []
chat_messages.append({"role": "user", "content": user_text})
try:
llm_messages = chat_to_llm_messages(chat_messages, system_prompt, file_context or "", user_text)
assistant = call_llm(llm_messages, temperature=temperature, max_tokens=max_tokens)
chat_messages.append({"role": "assistant", "content": assistant})
return chat_messages, ""
except Exception as e:
chat_messages.append({"role": "assistant", "content": f"β οΈ Error: {type(e).__name__}: {str(e)[:100]}"})
return chat_messages, ""
def on_clear():
return [], "", "No file selected"
# ========= SIMPLE UI =========
with gr.Blocks(title="AI Chat Assistant") as demo:
file_context_state = gr.State("")
chat_state = gr.State([])
# Header
with gr.Column():
gr.Markdown("# π€ AI Chat Assistant")
gr.Markdown("### Your intelligent assistant powered by Llama 3.1")
gr.Markdown(f"**Model:** `{CHAT_MODEL_ID.split('/')[-1]}`")
# Chat display
chatbot = gr.Chatbot(height=400, label="Chat History")
# File upload section
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### π Upload Document")
upload = gr.File(
label="",
file_count="single",
file_types=[".txt", ".pdf", ".md", ".csv", ".json", ".py"]
)
with gr.Column(scale=1):
gr.Markdown("### π€ Voice Input")
mic = gr.Audio(
sources=["microphone"],
type="filepath",
label=""
)
with gr.Column(scale=2):
file_info_display = gr.Textbox(
label="π File Status",
value="No file selected",
interactive=False
)
# Settings in accordion
with gr.Accordion("βοΈ Settings", open=False):
system_prompt = gr.Textbox(
value=DEFAULT_SYSTEM_PROMPT,
label="System Prompt",
lines=3
)
with gr.Row():
temperature = gr.Slider(
minimum=0.0,
maximum=1.5,
value=0.7,
step=0.05,
label="Temperature (Creativity)"
)
max_tokens = gr.Slider(
minimum=64,
maximum=1024,
value=400,
step=64,
label="Max Response Length"
)
# Input area
with gr.Row():
user_input = gr.Textbox(
placeholder="Type your message here...",
label="Your Message",
lines=3,
scale=4
)
with gr.Column(scale=1):
send_btn = gr.Button("π Send", variant="primary", size="lg")
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary")
# ========= EVENT HANDLERS =========
# File upload
upload.change(
fn=on_upload_file,
inputs=[chat_state, upload, file_context_state],
outputs=[chat_state, file_context_state, file_info_display]
).then(
fn=lambda x: x,
inputs=[chat_state],
outputs=[chatbot]
)
# Voice input
mic.change(
fn=on_record_audio,
inputs=[mic],
outputs=[user_input]
)
# Send message (button)
send_btn.click(
fn=on_send,
inputs=[chat_state, user_input, system_prompt, temperature, max_tokens, file_context_state],
outputs=[chat_state, user_input]
).then(
fn=lambda x: x,
inputs=[chat_state],
outputs=[chatbot]
)
# Send message (enter)
user_input.submit(
fn=on_send,
inputs=[chat_state, user_input, system_prompt, temperature, max_tokens, file_context_state],
outputs=[chat_state, user_input]
).then(
fn=lambda x: x,
inputs=[chat_state],
outputs=[chatbot]
)
# Clear chat
clear_btn.click(
fn=on_clear,
inputs=[],
outputs=[chat_state, file_context_state, file_info_display]
).then(
fn=lambda: [],
inputs=[],
outputs=[chatbot]
)
# Launch without CSS to avoid issues
demo.launch(share=False, server_name="0.0.0.0", server_port=7860) |