RamV commited on
Commit
28be0a0
·
1 Parent(s): 6fbea26

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import openai
3
+ import gradio as gr
4
+
5
+
6
+ # Set up OpenAI API credentials
7
+ api_key = os.environ.get("OPENAI_API_KEY")
8
+ openai.api_key = api_key
9
+
10
+ # Define a function to generate a response to user input
11
+ def generate_response(user_input):
12
+ if "who created you" in user_input.lower():
13
+ chat_response = "I was created by Ram.V"
14
+ elif "who is superstar" in user_input.lower():
15
+ chat_response = "The one and only * Superstar Rajinikanth * Thalaiva!"
16
+ elif "what's on 10th aug" in user_input.lower():
17
+ chat_response = "Jailer Movie releasing! Alappara Kelappurom Thalaivaru Nerandharam!"
18
+ elif "what is the weather like" in user_input.lower():
19
+ chat_response = "I'm sorry, but I don't have the ability to check the weather. Is there something else I can help you with?"
20
+ elif "how are you" in user_input.lower():
21
+ chat_response = "I'm a chatbot created by Ram.V. I don't have feelings, but thanks for asking. Hope you're well! :-)"
22
+ elif "what's your name" in user_input.lower():
23
+ chat_response = "My name is ChatRobo :-)"
24
+ else:
25
+ prompt = f"You said: {user_input}"
26
+
27
+
28
+ # Generate a response using the OpenAI API
29
+ response = openai.Completion.create(
30
+ model="text-davinci-003",
31
+ prompt=prompt,
32
+ temperature=0.9,
33
+ max_tokens=150,
34
+ top_p=1,
35
+ frequency_penalty=0,
36
+ presence_penalty=0.6,
37
+ stop=["Human:", "AI:"]
38
+ )
39
+
40
+ # Extract the response from the API output
41
+ chat_response = response.choices[0].text.strip()
42
+
43
+ return chat_response
44
+
45
+ # Define the function to handle the chat history
46
+ def openai_chat_history(input, history):
47
+ history = history or []
48
+ if input.strip() != "":
49
+ s = list(sum(history, ()))
50
+ s.append(input)
51
+ inp = ' '.join(s)
52
+ output = generate_response(inp)
53
+ history.append((input, output))
54
+ return history[-1][1]
55
+ else:
56
+ return ""
57
+
58
+ # Define the conversation prompt
59
+ conversation_prompt = "Welcome to ChatRobo, kindly type in your enquiries: "
60
+
61
+ # Set up the Gradio interface
62
+ block = gr.Interface(
63
+ fn=openai_chat_history,
64
+ inputs=[gr.inputs.Textbox(placeholder=conversation_prompt)],
65
+ outputs=[gr.outputs.Textbox(label="ChatRobo Output")]
66
+ )
67
+
68
+ # Launch the Gradio interface
69
+ block.launch()