Spaces:
Runtime error
Runtime error
update dropdown
Browse files
README.md
CHANGED
|
@@ -10,4 +10,8 @@ pinned: false
|
|
| 10 |
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 10 |
license: apache-2.0
|
| 11 |
---
|
| 12 |
|
| 13 |
+
```
|
| 14 |
+
export no_proxy="localhost, 127.0.0.1"
|
| 15 |
+
```
|
| 16 |
+
|
| 17 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -4,15 +4,16 @@ import json
|
|
| 4 |
from pymongo import MongoClient
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
|
| 8 |
# Initialize MongoDB client
|
| 9 |
-
client = MongoClient(os.environ['DB_URL'])
|
|
|
|
|
|
|
| 10 |
db = client['test']
|
| 11 |
collection = db['gradio']
|
| 12 |
|
| 13 |
def get_saved_data():
|
| 14 |
-
saved_data = collection.find({}, {"_id": 0, "
|
| 15 |
-
options = [
|
| 16 |
return options
|
| 17 |
|
| 18 |
def chat_with_gpt(question, api_key, temperature, system_message):
|
|
@@ -28,18 +29,24 @@ def chat_with_gpt(question, api_key, temperature, system_message):
|
|
| 28 |
assistant_reply = response['choices'][0]['message']['content']
|
| 29 |
return f"{assistant_reply}"
|
| 30 |
|
| 31 |
-
def
|
| 32 |
-
|
| 33 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
def update_textboxes(selected_data):
|
| 36 |
-
print(selected_data)
|
| 37 |
-
select_json = json.loads(selected_data)
|
| 38 |
-
print(select_json['question'], select_json['system_message'])
|
| 39 |
-
return select_json['question'], select_json['system_message']
|
| 40 |
|
| 41 |
with gr.Blocks() as app:
|
| 42 |
saved_data_dropdown = gr.Dropdown(get_saved_data(), label="Select Saved Data")
|
|
|
|
| 43 |
question = gr.Textbox(lines=2, placeholder="What's your question?", label="Question")
|
| 44 |
api_key = gr.Textbox(lines=1, placeholder="Your OpenAI API Key", label="API Key")
|
| 45 |
temperature = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Temperature")
|
|
@@ -52,7 +59,7 @@ with gr.Blocks() as app:
|
|
| 52 |
output = gr.Textbox(label="Result", interactive=False)
|
| 53 |
|
| 54 |
chat_btn.click(chat_with_gpt, inputs=[question, api_key, temperature, system_message], outputs=output)
|
| 55 |
-
save_btn.click(save_to_mongodb, inputs=[question, system_message], outputs=output)
|
| 56 |
-
saved_data_dropdown.select(update_textboxes, inputs=[saved_data_dropdown], outputs=[question, system_message])
|
| 57 |
|
| 58 |
app.launch()
|
|
|
|
| 4 |
from pymongo import MongoClient
|
| 5 |
import os
|
| 6 |
|
|
|
|
| 7 |
# Initialize MongoDB client
|
| 8 |
+
# client = MongoClient(os.environ['DB_URL'])
|
| 9 |
+
client = MongoClient("mongodb+srv://gradio-test:jiheng@cluster0.dvxynte.mongodb.net/?retryWrites=true&w=majority")
|
| 10 |
+
|
| 11 |
db = client['test']
|
| 12 |
collection = db['gradio']
|
| 13 |
|
| 14 |
def get_saved_data():
|
| 15 |
+
saved_data = collection.find({}, {"_id": 0, "name": 1})
|
| 16 |
+
options = [item['name'] for item in saved_data]
|
| 17 |
return options
|
| 18 |
|
| 19 |
def chat_with_gpt(question, api_key, temperature, system_message):
|
|
|
|
| 29 |
assistant_reply = response['choices'][0]['message']['content']
|
| 30 |
return f"{assistant_reply}"
|
| 31 |
|
| 32 |
+
def update_dropdown_choices():
|
| 33 |
+
new_choices = get_saved_data()
|
| 34 |
+
return gr.Dropdown.update(choices=new_choices)
|
| 35 |
+
|
| 36 |
+
def save_to_mongodb(name, question, system_message):
|
| 37 |
+
if not name.strip(): # Check if name is empty or just whitespace
|
| 38 |
+
return "Please enter a name.", None # Return a message and None to indicate no update for the dropdown
|
| 39 |
+
collection.insert_one({"name": name, "question": question, "system_message": system_message})
|
| 40 |
+
return "Saved to MongoDB.", update_dropdown_choices()
|
| 41 |
+
|
| 42 |
+
def update_textboxes(selected_name):
|
| 43 |
+
selected_data = collection.find_one({"name": selected_name}, {"_id": 0})
|
| 44 |
+
return selected_data['question'], selected_data['system_message'], selected_data['name']
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
with gr.Blocks() as app:
|
| 48 |
saved_data_dropdown = gr.Dropdown(get_saved_data(), label="Select Saved Data")
|
| 49 |
+
name = gr.Textbox(lines=1, placeholder="Name", label="Name")
|
| 50 |
question = gr.Textbox(lines=2, placeholder="What's your question?", label="Question")
|
| 51 |
api_key = gr.Textbox(lines=1, placeholder="Your OpenAI API Key", label="API Key")
|
| 52 |
temperature = gr.Slider(minimum=0, maximum=1, step=0.01, value=0.7, label="Temperature")
|
|
|
|
| 59 |
output = gr.Textbox(label="Result", interactive=False)
|
| 60 |
|
| 61 |
chat_btn.click(chat_with_gpt, inputs=[question, api_key, temperature, system_message], outputs=output)
|
| 62 |
+
save_btn.click(save_to_mongodb, inputs=[name, question, system_message], outputs=[output, saved_data_dropdown])
|
| 63 |
+
saved_data_dropdown.select(update_textboxes, inputs=[saved_data_dropdown], outputs=[question, system_message, name])
|
| 64 |
|
| 65 |
app.launch()
|