erndgn commited on
Commit
89a82e0
1 Parent(s): 8575bc3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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)
8
+
9
+ PLACEHOLDER = """
10
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
11
+ <img src="https://cdn.cosmosytu.online/images/cosmos_transparent.png" style="width: 80%; max-width: 550px; height: auto; opacity: 0.55; ">
12
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Cosmos LLaMa</h1>
13
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Bana herhangi bir şey sorabilirsiniz...</p>
14
+ </div>
15
+ """
16
+
17
+ css = """
18
+ h1 {
19
+ text-align: center;
20
+ display: block;
21
+ }
22
+ """
23
+
24
+ def convert_to_messages(message, history):
25
+ messages = []
26
+
27
+ for item in history:
28
+ messages.append({"role": "user", "content": item[0]})
29
+ messages.append({"role": "assistant", "content": item[1]})
30
+
31
+ messages.append({"role": "user", "content": message})
32
+
33
+ return messages
34
+
35
+ def chat_cosmosllama(message: str,
36
+ history: list
37
+ ) -> str:
38
+ messages = convert_to_messages(message, history)
39
+
40
+ headers = {
41
+ "Cosmos-Token": TOKEN,
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 = ""
49
+ for line in response.iter_lines():
50
+ if line:
51
+ decoded_line = line.decode('utf-8')
52
+ try:
53
+ json_line = json.loads(decoded_line[6:])
54
+
55
+ if "CosmosLlama" in json_line:
56
+ full_response += json_line['CosmosLlama']
57
+ yield full_response
58
+ except Exception as e:
59
+ print(e)
60
+ pass
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
+ gr.ChatInterface(
68
+ fn=chat_cosmosllama,
69
+ chatbot=chatbot,
70
+ fill_height=True,
71
+ cache_examples=False,
72
+ )
73
+
74
+ if __name__ == "__main__":
75
+ demo.launch()