Subha95 commited on
Commit
2468a73
Β·
verified Β·
1 Parent(s): db3aa39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -77
app.py CHANGED
@@ -1,86 +1,15 @@
1
- import os
2
  import gradio as gr
3
- from huggingface_hub import InferenceClient
4
 
 
 
5
 
6
- def respond(
7
- message,
8
- history: list[dict[str, str]],
9
- system_message,
10
- max_tokens,
11
- temperature,
12
- top_p,
13
- hf_token: gr.OAuthToken | None,
14
- ):
15
- """
16
- Chat handler for Hugging Face Inference API.
17
- Works with both HF Spaces (OAuth) and local execution (HF_TOKEN env var).
18
- """
19
-
20
- # βœ… Get token from Spaces OAuth or local env
21
- token = None
22
- if hf_token and getattr(hf_token, "token", None):
23
- token = hf_token.token
24
- else:
25
- token = os.getenv("HF_TOKEN")
26
-
27
- if not token:
28
- yield "❌ No Hugging Face token found. Please log in or set HF_TOKEN."
29
- return
30
-
31
- # βœ… Choose a model that supports Inference API
32
- model_id = "mistralai/Mistral-7B-Instruct-v0.2" # change if needed
33
- client = InferenceClient(token=token, model=model_id)
34
-
35
- # βœ… Build messages list
36
- messages = [{"role": "system", "content": system_message}]
37
- messages.extend(history)
38
- messages.append({"role": "user", "content": message})
39
-
40
- response = ""
41
-
42
- # βœ… Streaming response
43
- for message_chunk in client.chat_completion(
44
- messages,
45
- max_tokens=max_tokens,
46
- stream=True,
47
- temperature=temperature,
48
- top_p=top_p,
49
- ):
50
- choices = message_chunk.choices
51
- token = ""
52
- if len(choices) and choices[0].delta and choices[0].delta.content:
53
- token = choices[0].delta.content
54
-
55
- response += token
56
- yield response
57
-
58
-
59
- # βœ… Gradio UI
60
  chatbot = gr.ChatInterface(
61
  respond,
62
  type="messages",
63
- additional_inputs=[
64
- gr.Textbox(value="You are a helpful research assistant.", label="System message"),
65
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
66
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
67
- gr.Slider(
68
- minimum=0.1,
69
- maximum=1.0,
70
- value=0.95,
71
- step=0.05,
72
- label="Top-p (nucleus sampling)",
73
- ),
74
- ],
75
  )
76
 
77
- with gr.Blocks() as demo:
78
- with gr.Sidebar():
79
- gr.LoginButton()
80
- gr.Markdown("πŸ”‘ Log in with your Hugging Face account to run the model.")
81
- chatbot.render()
82
-
83
-
84
  if __name__ == "__main__":
85
- # βœ… share=True β†’ generates public link when running locally
86
- demo.launch(share=True)
 
 
1
  import gradio as gr
2
+ from ai_assistant import get_response
3
 
4
+ def respond(message, history):
5
+ return get_response(message, history)
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  chatbot = gr.ChatInterface(
8
  respond,
9
  type="messages",
10
+ title="Research Query Assistant",
11
+ description="Ask research questions, get answers from Mistral on Hugging Face."
 
 
 
 
 
 
 
 
 
 
12
  )
13
 
 
 
 
 
 
 
 
14
  if __name__ == "__main__":
15
+ chatbot.launch()