RamV commited on
Commit
e6612ee
·
1 Parent(s): 95701e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -44
app.py CHANGED
@@ -1,61 +1,35 @@
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("OPENAI_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, ()))
42
- s.append(input)
43
- inp = ' '.join(s)
44
- output = generate_response(inp)
45
- history.append((input, output))
46
- return history[-1][1]
47
- else:
48
- return ""
49
-
50
- # Define the conversation prompt
51
  conversation_prompt = "Welcome to ChatRobo, kindly type in your enquiries: "
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
- dir(gr.Interface)
 
60
 
61
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
 
4
+ def huggingface_chat_history(input, history=[]):
5
+ # create payload dictionary
 
 
 
 
6
  payload = {
7
+ "inputs": input,
8
  "parameters": {
9
+ "history": history
 
 
10
  }
11
  }
12
 
13
+ # make request to Hugging Face model API
 
 
 
 
 
 
14
  response = requests.post(
15
+ "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium",
16
+ headers={"Authorization": "OPENAI_API_KEY"},
17
+ json=payload
18
  )
 
19
 
20
+ # extract chatbot response from API response
21
+ chat_response = response.json()[0]['generated_text']
22
+ history.append([input, chat_response])
23
  return chat_response
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  conversation_prompt = "Welcome to ChatRobo, kindly type in your enquiries: "
26
 
27
+ iface = gr.Interface(
28
+ fn=huggingface_chat_history,
29
+ inputs=gr.inputs.Textbox(prompt=conversation_prompt),
30
+ outputs=gr.outputs.Textbox()
 
31
  )
32
+
33
+ iface.launch()
34
 
35