sunnynazir commited on
Commit
8214de5
·
verified ·
1 Parent(s): 8ec6623

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -7
app.py CHANGED
@@ -1,8 +1,23 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
-
4
- # Load the model with text-generation task
5
- chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Set up Streamlit UI
8
  st.title("Learning Chatbot")
@@ -22,10 +37,10 @@ if user_message:
22
  # Combine the history for context in the conversation
23
  conversation_history = " ".join(st.session_state.history)
24
 
25
- # Generate the response
26
- bot_response = chatbot(conversation_history, max_length=1000, num_return_sequences=1)[0]['generated_text']
27
 
28
- # Display the bot's response
29
  st.session_state.history.append(f"Bot: {bot_response}")
30
 
31
  # Show conversation history
 
1
  import streamlit as st
2
+ import requests
3
+
4
+ # Set your Hugging Face API key here
5
+ API_KEY = 'your_huggingface_api_key'
6
+ MODEL_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
7
+
8
+ # Function to get chatbot response from Hugging Face API
9
+ def query_huggingface_api(message):
10
+ headers = {"Authorization": f"Bearer {API_KEY}"}
11
+ payload = {"inputs": message}
12
+
13
+ response = requests.post(MODEL_URL, headers=headers, json=payload)
14
+ response_json = response.json()
15
+
16
+ # Extract and return the chatbot response
17
+ if isinstance(response_json, list):
18
+ return response_json[0]['generated_text']
19
+ else:
20
+ return "Error: Unable to get response from the model."
21
 
22
  # Set up Streamlit UI
23
  st.title("Learning Chatbot")
 
37
  # Combine the history for context in the conversation
38
  conversation_history = " ".join(st.session_state.history)
39
 
40
+ # Query the Hugging Face API to get the response
41
+ bot_response = query_huggingface_api(conversation_history)
42
 
43
+ # Append the bot's response to the history
44
  st.session_state.history.append(f"Bot: {bot_response}")
45
 
46
  # Show conversation history