Spaces:
Sleeping
Sleeping
add: upload gradio
Browse files- app.py +118 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
from typing import List, Tuple, Optional, Dict, Union
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
TITLE = """<h1 align="center">Insight LLM 💬</h1>"""
|
| 9 |
+
SUBTITLE = """<h2 align="center">Effortlessly analyze and compare Thai stocks with advanced LLM insights.</h2>"""
|
| 10 |
+
DUPLICATE = """
|
| 11 |
+
<div style="text-align: center; display: flex; justify-content: center; align-items: center;">
|
| 12 |
+
<span>Duplicate the Space and run securely with your API KEY.
|
| 13 |
+
</span>
|
| 14 |
+
</div>
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
AVATAR_IMAGES = (
|
| 18 |
+
None,
|
| 19 |
+
"https://media.roboflow.com/spaces/gemini-icon.png"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
|
| 23 |
+
|
| 24 |
+
def preprocess_chat_history(
|
| 25 |
+
history: CHAT_HISTORY
|
| 26 |
+
) -> List[Dict[str, Union[str, List[str]]]]:
|
| 27 |
+
messages = []
|
| 28 |
+
for user_message, model_message in history:
|
| 29 |
+
if isinstance(user_message, tuple):
|
| 30 |
+
pass
|
| 31 |
+
elif user_message is not None:
|
| 32 |
+
messages.append({'role': 'user', 'parts': [user_message]})
|
| 33 |
+
if model_message is not None:
|
| 34 |
+
messages.append({'role': 'model', 'parts': [model_message]})
|
| 35 |
+
return messages
|
| 36 |
+
|
| 37 |
+
def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
| 38 |
+
if text_prompt:
|
| 39 |
+
chatbot.append((text_prompt, None))
|
| 40 |
+
return "", chatbot
|
| 41 |
+
|
| 42 |
+
def bot(
|
| 43 |
+
chatbot: CHAT_HISTORY
|
| 44 |
+
):
|
| 45 |
+
if len(chatbot) == 0:
|
| 46 |
+
return chatbot
|
| 47 |
+
|
| 48 |
+
if chatbot[-1][0] and isinstance(chatbot[-1][0], str):
|
| 49 |
+
text_prompt = chatbot[-1][0]
|
| 50 |
+
try:
|
| 51 |
+
response = requests.get(
|
| 52 |
+
"https://api-obon.conf.in.th/team15/query",
|
| 53 |
+
params={"text": text_prompt}
|
| 54 |
+
)
|
| 55 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 56 |
+
|
| 57 |
+
try:
|
| 58 |
+
response_json = response.json()
|
| 59 |
+
chatbot[-1][1] = response_json.get("text", "")
|
| 60 |
+
except ValueError:
|
| 61 |
+
chatbot[-1][1] = f"{response.text}"
|
| 62 |
+
except requests.exceptions.RequestException as e:
|
| 63 |
+
chatbot[-1][1] = f"Error: {e}"
|
| 64 |
+
else:
|
| 65 |
+
chatbot[-1][1] = "Invalid input"
|
| 66 |
+
|
| 67 |
+
return chatbot
|
| 68 |
+
|
| 69 |
+
chatbot_component = gr.Chatbot(
|
| 70 |
+
label='API Chatbot',
|
| 71 |
+
bubble_full_width=False,
|
| 72 |
+
avatar_images=AVATAR_IMAGES,
|
| 73 |
+
scale=2,
|
| 74 |
+
height=400
|
| 75 |
+
)
|
| 76 |
+
text_prompt_component = gr.Textbox(
|
| 77 |
+
placeholder="Hi there! [press Enter]", show_label=False, autofocus=True, scale=8
|
| 78 |
+
)
|
| 79 |
+
run_button_component = gr.Button(value="Run", variant="primary", scale=1)
|
| 80 |
+
|
| 81 |
+
user_inputs = [
|
| 82 |
+
text_prompt_component,
|
| 83 |
+
chatbot_component
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
bot_inputs = [
|
| 87 |
+
chatbot_component
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
with gr.Blocks() as demo:
|
| 91 |
+
gr.HTML(TITLE)
|
| 92 |
+
gr.HTML(SUBTITLE)
|
| 93 |
+
gr.HTML(DUPLICATE)
|
| 94 |
+
with gr.Column():
|
| 95 |
+
chatbot_component.render()
|
| 96 |
+
with gr.Row():
|
| 97 |
+
text_prompt_component.render()
|
| 98 |
+
run_button_component.render()
|
| 99 |
+
|
| 100 |
+
run_button_component.click(
|
| 101 |
+
fn=user,
|
| 102 |
+
inputs=user_inputs,
|
| 103 |
+
outputs=[text_prompt_component, chatbot_component],
|
| 104 |
+
queue=False
|
| 105 |
+
).then(
|
| 106 |
+
fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
text_prompt_component.submit(
|
| 110 |
+
fn=user,
|
| 111 |
+
inputs=user_inputs,
|
| 112 |
+
outputs=[text_prompt_component, chatbot_component],
|
| 113 |
+
queue=False
|
| 114 |
+
).then(
|
| 115 |
+
fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
demo.queue(max_size=99).launch(debug=False, show_error=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests
|
| 2 |
+
gradio
|
| 3 |
+
Pillow
|