Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,41 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
# Set up
|
| 6 |
-
api_key = os.environ.get("
|
| 7 |
-
openai.api_key = api_key
|
| 8 |
|
| 9 |
# Define a function to generate a response to user input
|
| 10 |
def generate_response(user_input):
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
return chat_response
|
| 35 |
|
| 36 |
# Define the function to handle the chat history
|
| 37 |
-
def
|
| 38 |
history = history or []
|
| 39 |
if input.strip() != "":
|
| 40 |
s = list(sum(history, ()))
|
|
@@ -51,11 +52,9 @@ conversation_prompt = "Welcome to ChatRobo, kindly type in your enquiries: "
|
|
| 51 |
|
| 52 |
# Set up the Gradio interface
|
| 53 |
block = gr.Interface(
|
| 54 |
-
fn=
|
| 55 |
inputs=[gr.inputs.Textbox(placeholder=conversation_prompt)],
|
| 56 |
outputs=[gr.outputs.Textbox(label="ChatRobo Output")]
|
| 57 |
)
|
| 58 |
-
|
| 59 |
-
# Launch the Gradio interface
|
| 60 |
-
block.launch()
|
| 61 |
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Set up Hugging Face API credentials
|
| 7 |
+
api_key = os.environ.get("HUGGINGFACE_API_KEY")
|
|
|
|
| 8 |
|
| 9 |
# Define a function to generate a response to user input
|
| 10 |
def generate_response(user_input):
|
| 11 |
+
# Construct the payload for the API request
|
| 12 |
+
payload = {
|
| 13 |
+
"inputs": user_input,
|
| 14 |
+
"parameters": {
|
| 15 |
+
"max_new_tokens": 100,
|
| 16 |
+
"temperature": 0.5,
|
| 17 |
+
"model": "microsoft/DialoGPT-medium"
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Set up the request headers with the API key
|
| 22 |
+
headers = {
|
| 23 |
+
"Authorization": f"Bearer {api_key}",
|
| 24 |
+
"Content-Type": "application/json"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Make the API request and extract the response
|
| 28 |
+
response = requests.post(
|
| 29 |
+
"https://api-inference.huggingface.co/models/openai/dialogue",
|
| 30 |
+
headers=headers,
|
| 31 |
+
data=json.dumps(payload)
|
| 32 |
+
)
|
| 33 |
+
chat_response = response.json()["generated_text"].strip()
|
| 34 |
|
| 35 |
return chat_response
|
| 36 |
|
| 37 |
# Define the function to handle the chat history
|
| 38 |
+
def huggingface_chat_history(input, history):
|
| 39 |
history = history or []
|
| 40 |
if input.strip() != "":
|
| 41 |
s = list(sum(history, ()))
|
|
|
|
| 52 |
|
| 53 |
# Set up the Gradio interface
|
| 54 |
block = gr.Interface(
|
| 55 |
+
fn=huggingface_chat_history,
|
| 56 |
inputs=[gr.inputs.Textbox(placeholder=conversation_prompt)],
|
| 57 |
outputs=[gr.outputs.Textbox(label="ChatRobo Output")]
|
| 58 |
)
|
| 59 |
+
block.run()
|
|
|
|
|
|
|
| 60 |
|