Rishi Srivastava commited on
Commit Β·
05b1030
1
Parent(s): 57edb00
push to the space
Browse files- app.py +68 -29
- requirements.txt +7 -1
app.py
CHANGED
|
@@ -1,44 +1,83 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
hf_token = os.environ.get("HF_TOKEN")
|
| 6 |
-
client = InferenceClient("
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
demo = gr.ChatInterface(
|
| 34 |
respond,
|
| 35 |
additional_inputs=[
|
| 36 |
-
gr.Textbox(value="
|
| 37 |
-
gr.Slider(minimum=
|
| 38 |
-
gr.Slider(minimum=0.1, maximum=
|
| 39 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 40 |
],
|
|
|
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
if __name__ == "__main__":
|
| 44 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
+
# -------------------------------
|
| 7 |
+
# π Hugging Face API setup
|
| 8 |
+
# -------------------------------
|
| 9 |
hf_token = os.environ.get("HF_TOKEN")
|
| 10 |
+
client = InferenceClient(model="deepseek-ai/deepseek-v3", token=hf_token)
|
| 11 |
+
|
| 12 |
+
# -------------------------------
|
| 13 |
+
# π Google Doc Fetching
|
| 14 |
+
# -------------------------------
|
| 15 |
+
def fetch_google_doc_text(doc_id: str) -> str:
|
| 16 |
+
export_url = f"https://docs.google.com/document/d/{doc_id}/export?format=txt"
|
| 17 |
+
response = requests.get(export_url)
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
return response.text.strip()
|
| 20 |
+
else:
|
| 21 |
+
return f"β οΈ Unable to fetch document. Status code: {response.status_code}"
|
| 22 |
+
|
| 23 |
+
# Replace with your actual Google Doc ID
|
| 24 |
+
GOOGLE_DOC_ID = "1s9_2SsGxj5taxNMw_dIq0awSgzQEu-EIW1P0BmZ8zo4"
|
| 25 |
+
doc_context = fetch_google_doc_text(GOOGLE_DOC_ID)
|
| 26 |
+
doc_context = doc_context[:12000] # Optional: truncate to fit into model context
|
| 27 |
+
#print(doc_context)
|
| 28 |
|
| 29 |
+
# -------------------------------
|
| 30 |
+
# π¬ Chatbot: Use context only
|
| 31 |
+
# -------------------------------
|
| 32 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 33 |
+
try:
|
| 34 |
+
system_prompt = (
|
| 35 |
+
"You are a helpful assistant. Only answer questions using the context provided. "
|
| 36 |
+
"If the answer is not in the context, respond with: 'I can't answer this question.'"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
full_context = f"{system_prompt}\n\nContext:\n{doc_context}"
|
| 40 |
+
#print(doc_context)
|
| 41 |
+
|
| 42 |
+
messages = [
|
| 43 |
+
{"role": "system", "content": full_context},
|
| 44 |
+
{"role": "user", "content": message}
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
response = ""
|
| 48 |
+
for chunk in client.chat_completion(
|
| 49 |
+
messages=messages,
|
| 50 |
+
max_tokens=max_tokens,
|
| 51 |
+
temperature=temperature,
|
| 52 |
+
top_p=top_p,
|
| 53 |
+
stream=True
|
| 54 |
+
):
|
| 55 |
+
token = chunk.choices[0].delta.content
|
| 56 |
+
if token:
|
| 57 |
+
response += token
|
| 58 |
+
yield response
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
yield f"β Error: {e}"
|
| 62 |
|
| 63 |
+
# -------------------------------
|
| 64 |
+
# ποΈ Gradio UI Setup
|
| 65 |
+
# -------------------------------
|
| 66 |
demo = gr.ChatInterface(
|
| 67 |
respond,
|
| 68 |
additional_inputs=[
|
| 69 |
+
gr.Textbox(value="(ignored in this version)", label="System message"),
|
| 70 |
+
gr.Slider(minimum=64, maximum=4096, value=1024, step=64, label="Max new tokens"),
|
| 71 |
+
gr.Slider(minimum=0.1, maximum=1.5, value=0.3, step=0.1, label="Temperature"),
|
| 72 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 73 |
],
|
| 74 |
+
title="π¬ Beiing Human FAQ Chatbot",
|
| 75 |
+
description="Ask questions based only on the content of the shared Google Doc FAQ. "
|
| 76 |
+
"If the answer isn't in the document, the bot will say so.",
|
| 77 |
)
|
| 78 |
|
| 79 |
+
# -------------------------------
|
| 80 |
+
# π Launch App
|
| 81 |
+
# -------------------------------
|
| 82 |
if __name__ == "__main__":
|
| 83 |
+
demo.launch(debug=True, share=True)
|
requirements.txt
CHANGED
|
@@ -1 +1,7 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
openpyxl
|
| 3 |
+
PyPDF2
|
| 4 |
+
pandas
|
| 5 |
+
pillow # For PIL (Python Imaging Library)
|
| 6 |
+
pytesseract
|
| 7 |
+
huggingface_hub
|