Add state management and localization
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import json
|
4 |
import os
|
|
|
5 |
|
6 |
ENDPOINT = os.environ.get("ENDPOINT", None)
|
7 |
TOKEN = os.environ.get("TOKEN", None)
|
@@ -33,7 +34,8 @@ def convert_to_messages(message, history):
|
|
33 |
return messages
|
34 |
|
35 |
def chat_cosmosllama(message: str,
|
36 |
-
history: list
|
|
|
37 |
) -> str:
|
38 |
messages = convert_to_messages(message, history)
|
39 |
|
@@ -42,7 +44,7 @@ def chat_cosmosllama(message: str,
|
|
42 |
"Content-Type": "application/json"
|
43 |
}
|
44 |
|
45 |
-
response = requests.post(f"https://model.cosmosytu.online{ENDPOINT}", json={"messages": messages}, headers=headers, stream=True)
|
46 |
|
47 |
if response.status_code == 200:
|
48 |
full_response = ""
|
@@ -61,15 +63,34 @@ def chat_cosmosllama(message: str,
|
|
61 |
else:
|
62 |
yield "Bir hata oluştu."
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Cosmos LLaMa')
|
65 |
|
66 |
with gr.Blocks(fill_height=True, css=css) as demo:
|
67 |
-
|
|
|
|
|
|
|
68 |
fn=chat_cosmosllama,
|
69 |
chatbot=chatbot,
|
70 |
fill_height=True,
|
71 |
cache_examples=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
)
|
73 |
|
|
|
|
|
|
|
74 |
if __name__ == "__main__":
|
75 |
demo.launch()
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
import os
|
5 |
+
import uuid
|
6 |
|
7 |
ENDPOINT = os.environ.get("ENDPOINT", None)
|
8 |
TOKEN = os.environ.get("TOKEN", None)
|
|
|
34 |
return messages
|
35 |
|
36 |
def chat_cosmosllama(message: str,
|
37 |
+
history: list,
|
38 |
+
conversation_id: str
|
39 |
) -> str:
|
40 |
messages = convert_to_messages(message, history)
|
41 |
|
|
|
44 |
"Content-Type": "application/json"
|
45 |
}
|
46 |
|
47 |
+
response = requests.post(f"https://model.cosmosytu.online{ENDPOINT}", json={"messages": messages, "conversation_id": conversation_id}, headers=headers, stream=True)
|
48 |
|
49 |
if response.status_code == 200:
|
50 |
full_response = ""
|
|
|
63 |
else:
|
64 |
yield "Bir hata oluştu."
|
65 |
|
66 |
+
def get_conversation_id():
|
67 |
+
return str(uuid.uuid4())
|
68 |
+
|
69 |
+
def reset_conversation():
|
70 |
+
new_conversation_id = get_conversation_id()
|
71 |
+
return new_conversation_id
|
72 |
+
|
73 |
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Cosmos LLaMa')
|
74 |
|
75 |
with gr.Blocks(fill_height=True, css=css) as demo:
|
76 |
+
|
77 |
+
conversation_id_state = gr.State(value=get_conversation_id)
|
78 |
+
|
79 |
+
interface = gr.ChatInterface(
|
80 |
fn=chat_cosmosllama,
|
81 |
chatbot=chatbot,
|
82 |
fill_height=True,
|
83 |
cache_examples=False,
|
84 |
+
additional_inputs=[conversation_id_state],
|
85 |
+
undo_btn=None,
|
86 |
+
retry_btn="🔄 Tekrar Cevapla",
|
87 |
+
clear_btn="🗑️ Temizle",
|
88 |
+
submit_btn="Gönder",
|
89 |
+
stop_btn="Durdur"
|
90 |
)
|
91 |
|
92 |
+
interface.retry_btn.click(fn=reset_conversation, inputs=[], outputs=[conversation_id_state])
|
93 |
+
interface.textbox.placeholder="Mesajınızı buraya giriniz..."
|
94 |
+
|
95 |
if __name__ == "__main__":
|
96 |
demo.launch()
|