Rishi Srivastava commited on
Commit
05b1030
Β·
1 Parent(s): 57edb00

push to the space

Browse files
Files changed (2) hide show
  1. app.py +68 -29
  2. 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("Qwen/Qwen1.5-32B-Chat", token=hf_token)
7
-
8
- def build_prompt(system_message, history, user_message):
9
- prompt = f"<|im_start|>system\n{system_message}<|im_end|>\n"
10
- for user, assistant in history:
11
- if user:
12
- prompt += f"<|im_start|>user\n{user}<|im_end|>\n"
13
- if assistant:
14
- prompt += f"<|im_start|>assistant\n{assistant}<|im_end|>\n"
15
- prompt += f"<|im_start|>user\n{user_message}<|im_end|>\n<|im_start|>assistant\n"
16
- return prompt
 
 
 
 
 
 
 
17
 
 
 
 
18
  def respond(message, history, system_message, max_tokens, temperature, top_p):
19
- prompt = build_prompt(system_message, history, message)
20
- response = ""
21
-
22
- for token in client.text_generation(
23
- prompt=prompt,
24
- max_new_tokens=max_tokens,
25
- temperature=temperature,
26
- top_p=top_p,
27
- stream=True,
28
- ):
29
- if token.token: # some tokens may be empty (e.g., control tokens)
30
- response += token.token
31
- yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
 
 
 
33
  demo = gr.ChatInterface(
34
  respond,
35
  additional_inputs=[
36
- gr.Textbox(value="You are a helpful assistant for construction CFOs.", label="System message"),
37
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
38
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
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
- huggingface_hub==0.25.2
 
 
 
 
 
 
 
1
+ gradio
2
+ openpyxl
3
+ PyPDF2
4
+ pandas
5
+ pillow # For PIL (Python Imaging Library)
6
+ pytesseract
7
+ huggingface_hub