RamV commited on
Commit
11bb600
·
1 Parent(s): 8ddeecc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -31
app.py CHANGED
@@ -1,40 +1,41 @@
1
  import os
2
- import openai
 
3
  import gradio as gr
4
 
5
- # Set up OpenAI API credentials
6
- api_key = os.environ.get("OPENAI_API_KEY")
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
- # Check if user input contains the phrase "who created you"
12
- if "who created you" in user_input.lower():
13
- # Generate a response that includes your name
14
- chat_response = "I was created by RamV!"
15
- else:
16
- # Construct the prompt with the user input
17
- prompt = f"You said: {user_input}"
18
-
19
- # Generate a response using the OpenAI API
20
- response = openai.Completion.create(
21
- model="text-davinci-003",
22
- prompt=prompt,
23
- temperature=0.9,
24
- max_tokens=150,
25
- top_p=1,
26
- frequency_penalty=0,
27
- presence_penalty=0.6,
28
- stop=["Human:", "AI:"]
29
- )
30
-
31
- # Extract the response from the API output
32
- chat_response = response.choices[0].text.strip()
 
33
 
34
  return chat_response
35
 
36
  # Define the function to handle the chat history
37
- def openai_chat_history(input, history):
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=openai_chat_history,
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