AnkitkumarMalde commited on
Commit
6cf9896
·
verified ·
1 Parent(s): 1bb7e24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -41
app.py CHANGED
@@ -1,45 +1,41 @@
 
 
 
 
1
  import gradio as gr
2
- from transformers import pipeline
3
- import torch
4
-
5
- # 1. Initialize the Model
6
- # We use 'device_map="auto"' to handle hardware efficiently
7
- # If using a specific model from Kaggle, replace "microsoft/DialoGPT-medium" with your model ID
8
- model_id = "microsoft/DialoGPT-medium"
9
- chatbot_pipeline = pipeline("text-generation", model=model_id, device_map="auto")
10
-
11
- # 2. Define the Inference Logic
12
- def chat_function(message, history):
13
- """
14
- message: The current user input
15
- history: A list of previous [user_msg, bot_msg] pairs provided by Gradio
16
- """
17
- # Combine history into a single string for context (simplified)
18
- context = ""
19
  for user_msg, bot_msg in history:
20
- context += f"User: {user_msg}\nAssistant: {bot_msg}\n"
21
-
22
- context += f"User: {message}\nAssistant:"
23
-
24
- # Generate response
25
- output = chatbot_pipeline(context, max_new_tokens=100, pad_token_id=50256)
26
-
27
- # Extract only the newly generated text
28
- generated_text = output[0]['generated_text']
29
- response = generated_text.split("Assistant:")[-1].strip()
30
-
31
- return response
32
-
33
- # 3. Build the Gradio UI
34
- # 'ChatInterface' automatically handles the text box and 'Send' button logic
35
- demo = gr.ChatInterface(
36
- fn=chat_function,
37
- title="My Kaggle-to-HF Chatbot",
38
- description="Ask me anything! This bot was developed in Kaggle and hosted on Hugging Face.",
39
- examples=["Hello!", "What is artificial intelligence?", "Tell me a joke."],
40
- theme="soft"
41
- )
42
-
43
- # 4. Launch
44
  if __name__ == "__main__":
45
  demo.launch()
 
1
+ import os
2
+ # This automatically picks up the secret you added in Settings
3
+ token = os.getenv("admin_token")
4
+
5
  import gradio as gr
6
+ from huggingface_hub import InferenceClient
7
+ import os
8
+
9
+ # 1. Initialize Client (Uses HF servers, saves your Space's memory)
10
+ # This will automatically use the HF_TOKEN secret you added to Settings
11
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
12
+
13
+ def respond(message, history):
14
+ messages = []
15
+
16
+ # Build history for the API
 
 
 
 
 
 
17
  for user_msg, bot_msg in history:
18
+ messages.append({"role": "user", "content": user_msg})
19
+ messages.append({"role": "assistant", "content": bot_msg})
20
+
21
+ messages.append({"role": "user", "content": message})
22
+
23
+ response = ""
24
+
25
+ # Stream the response back
26
+ for message in client.chat_completion(
27
+ messages,
28
+ max_tokens=512,
29
+ stream=True,
30
+ temperature=0.7,
31
+ top_p=0.95,
32
+ ):
33
+ token = message.choices[0].delta.content
34
+ response += token
35
+ yield response
36
+
37
+ # 2. Simplified UI
38
+ demo = gr.ChatInterface(respond)
39
+
 
 
40
  if __name__ == "__main__":
41
  demo.launch()