Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,25 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import json
|
3 |
-
import os
|
4 |
-
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
|
5 |
-
from langchain import OpenAI
|
6 |
-
import sys
|
7 |
|
8 |
-
|
9 |
|
10 |
-
|
|
|
|
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
|
22 |
-
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
|
23 |
-
|
24 |
-
|
25 |
-
'''
|
26 |
-
|
27 |
-
import tkinter as tk
|
28 |
-
from llama_index import GPTSimpleVectorIndex, LLMPredictor, PromptHelper
|
29 |
-
from langchain import OpenAI
|
30 |
-
from IPython.display import Markdown, display
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
# Define the GUI
|
35 |
-
class ChatBotGUI:
|
36 |
-
def __init__(self, master):
|
37 |
-
self.master = master
|
38 |
-
master.title("Chat Bot")
|
39 |
-
|
40 |
-
# Create a label and an entry for the question
|
41 |
-
self.label = tk.Label(master, text="Ask me anything:")
|
42 |
-
self.label.pack()
|
43 |
-
|
44 |
-
self.entry = tk.Entry(master)
|
45 |
-
self.entry.pack()
|
46 |
-
|
47 |
-
# Create a button to submit the question
|
48 |
-
self.button = tk.Button(master, text="Submit", command=self.submit_question)
|
49 |
-
self.button.pack()
|
50 |
-
|
51 |
-
# Create a text box to display the response
|
52 |
-
self.textbox = tk.Text(master)
|
53 |
-
self.textbox.pack()
|
54 |
-
|
55 |
-
def submit_question(self):
|
56 |
-
question = self.entry.get()
|
57 |
-
response = ask_ai(question)
|
58 |
-
self.textbox.insert(tk.END, "You: " + question + "\n")
|
59 |
-
self.textbox.insert(tk.END, "Bot: " + response + "\n\n")
|
60 |
-
self.entry.delete(0, tk.END)
|
61 |
-
|
62 |
-
# Create an instance of the GUI and start the main loop
|
63 |
-
root = tk.Tk()
|
64 |
-
chatbot_gui = ChatBotGUI(root)
|
65 |
-
root.mainloop()
|
66 |
-
'''
|
67 |
-
|
68 |
-
|
69 |
-
os.environ["OPENAI_API_KEY"] = "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"
|
70 |
-
construct_index("data")
|
71 |
-
def ask_ai(question,openai_api_key):
|
72 |
-
|
73 |
-
if openai_api_key == "":
|
74 |
-
os.environ["OPENAI_API_KEY"] = "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"
|
75 |
-
else:
|
76 |
-
os.environ["OPENAI_API_KEY"] = openai_api_key
|
77 |
-
construct_index("data")
|
78 |
-
|
79 |
-
index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
80 |
-
response = index.query(question, response_mode="compact")
|
81 |
-
return response.response
|
82 |
-
|
83 |
-
|
84 |
-
api_key = gr.inputs.Textbox(label="Paste OPENAI API Key (Or left it blank to use default api)")
|
85 |
-
iface = gr.Interface(fn=ask_ai, inputs=["text", api_key], outputs="text", title="Chatbot")
|
86 |
-
|
87 |
-
|
88 |
-
iface.launch()
|
|
|
1 |
+
import openai
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
openai.api_key = "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"
|
5 |
|
6 |
+
messages = [
|
7 |
+
{"role": "system", "content": "You are a helpful and kind AI Assistant."},
|
8 |
+
]
|
9 |
|
10 |
+
def chatbot(input):
|
11 |
+
if input:
|
12 |
+
messages.append({"role": "user", "content": input})
|
13 |
+
chat = openai.ChatCompletion.create(
|
14 |
+
model="gpt-3.5-turbo", messages=messages
|
15 |
+
)
|
16 |
+
reply = chat.choices[0].message.content
|
17 |
+
messages.append({"role": "assistant", "content": reply})
|
18 |
+
return reply
|
19 |
|
20 |
+
inputs = gr.inputs.Textbox(lines=7, label="Chat with AI")
|
21 |
+
outputs = gr.outputs.Textbox(label="Reply")
|
22 |
|
23 |
+
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Chatbot",
|
24 |
+
description="Ask anything you want",
|
25 |
+
theme="compact").launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|