comfyri commited on
Commit
0115538
ยท
verified ยท
1 Parent(s): a22bc2e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -0
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import SentenceTransformer
3
+ import faiss
4
+ import os
5
+
6
+ # ============================
7
+ # 1) ๋Œ€ํ™” ๋ฐ์ดํ„ฐ ๋กœ๋“œ
8
+ # ============================
9
+ DATA_FILE = "data/conversation.txt"
10
+
11
+ if not os.path.exists(DATA_FILE):
12
+ os.makedirs("data", exist_ok=True)
13
+ with open(DATA_FILE, "w", encoding="utf-8") as f:
14
+ f.write("์—ฌ๊ธฐ์— ๋Œ€ํ™” ๋‚ด์šฉ์„ ๋„ฃ์œผ์„ธ์š”.\n")
15
+
16
+ with open(DATA_FILE, "r", encoding="utf-8") as f:
17
+ text = f.read()
18
+
19
+ # ============================
20
+ # 2) ์ฒญํฌ ์ฒ˜๋ฆฌ
21
+ # ============================
22
+ def chunk_text(text, chunk_size=500):
23
+ lines = text.split("\n")
24
+ chunks, cur, length = [], [], 0
25
+
26
+ for line in lines:
27
+ line = line.strip()
28
+ if not line:
29
+ continue
30
+ if length + len(line) > chunk_size:
31
+ chunks.append("\n".join(cur))
32
+ cur = []
33
+ length = 0
34
+ cur.append(line)
35
+ length += len(line)
36
+
37
+ if cur:
38
+ chunks.append("\n".join(cur))
39
+
40
+ return chunks
41
+
42
+ chunks = chunk_text(text)
43
+
44
+ # ============================
45
+ # 3) ์ž„๋ฒ ๋”ฉ + ์ธ๋ฑ์Šค
46
+ # ============================
47
+ model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
48
+ embeddings = model.encode(chunks)
49
+
50
+ dimension = embeddings.shape[1]
51
+ index = faiss.IndexFlatL2(dimension)
52
+ index.add(embeddings)
53
+
54
+ def retrieve(query, top_k=3):
55
+ qvec = model.encode([query])
56
+ dist, idxs = index.search(qvec, top_k)
57
+ return "\n\n".join(chunks[i] for i in idxs[0] if 0 <= i < len(chunks))
58
+
59
+ # ============================
60
+ # 4) LLM API ์ค€๋น„
61
+ # ============================
62
+ import openai
63
+ import google.generativeai as genai
64
+ import requests
65
+ import json
66
+
67
+ # OPENAI (GPT)
68
+ if os.getenv("OPENAI_API_KEY"):
69
+ openai.api_key = os.getenv("OPENAI_API_KEY")
70
+
71
+ # GEMINI
72
+ if os.getenv("GEMINI_API_KEY"):
73
+ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
74
+
75
+ # GROK (xAI)
76
+ GROK_KEY = os.getenv("GROK_API_KEY")
77
+ GROK_URL = "https://api.x.ai/v1/chat/completions"
78
+
79
+ # ============================
80
+ # 5) ๋ชจ๋ธ๋ณ„ ํ˜ธ์ถœ ํ•จ์ˆ˜
81
+ # ============================
82
+ def call_gpt(prompt):
83
+ response = openai.ChatCompletion.create(
84
+ model="gpt-4o-mini",
85
+ messages=[{"role": "user", "content": prompt}]
86
+ )
87
+ return response["choices"][0]["message"]["content"]
88
+
89
+ def call_gemini(prompt):
90
+ model = genai.GenerativeModel("gemini-1.5-flash")
91
+ response = model.generate_content(prompt)
92
+ return response.text
93
+
94
+ def call_grok(prompt):
95
+ headers = {
96
+ "Authorization": f"Bearer {GROK_KEY}",
97
+ "Content-Type": "application/json"
98
+ }
99
+ data = {
100
+ "model": "grok-1.5",
101
+ "messages": [{"role": "user", "content": prompt}]
102
+ }
103
+ r = requests.post(GROK_URL, headers=headers, data=json.dumps(data))
104
+ out = r.json()
105
+ return out["choices"][0]["message"]["content"]
106
+
107
+ # ============================
108
+ # 6) ๋ฉ”์ธ ์ฑ— ํ•จ์ˆ˜
109
+ # ============================
110
+ def chat(query, history, model_name):
111
+ related = retrieve(query)
112
+
113
+ prompt = f"""
114
+ ๋‹น์‹ ์€ ์—…์Šค์ผ€์ผ/ONNX/TensorRT ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค.
115
+ ์•„๋ž˜๋Š” ํ”„๋กœ์ ํŠธ ํ•ต์‹ฌ ๋ฌธ๋งฅ์ž…๋‹ˆ๋‹ค:
116
+
117
+ {related}
118
+
119
+ ์œ„ ๋ฌธ๋งฅ์„ ์ฐธ๊ณ ํ•ด ๋‹ค์Œ ์งˆ๋ฌธ์— ๋‹ตํ•˜์„ธ์š”:
120
+
121
+ ์งˆ๋ฌธ: {query}
122
+ """
123
+
124
+ if model_name == "GPT-4o-mini":
125
+ answer = call_gpt(prompt)
126
+ elif model_name == "Gemini":
127
+ answer = call_gemini(prompt)
128
+ elif model_name == "Grok":
129
+ answer = call_grok(prompt)
130
+ else:
131
+ answer = "์ง€์›๋˜์ง€ ์•Š๋Š” LLM์ž…๋‹ˆ๋‹ค."
132
+
133
+ history.append((query, answer))
134
+ return history, history
135
+
136
+ # ============================
137
+ # 7) UI
138
+ # ============================
139
+ with gr.Blocks(title="Upscale Frog ๐Ÿธ RAG") as app:
140
+ gr.Markdown("## ๐Ÿธ Upscale Frog โ€” Multi-LLM RAG Engine")
141
+
142
+ with gr.Row():
143
+ llm_selector = gr.Dropdown(
144
+ ["GPT-4o-mini", "Gemini", "Grok"],
145
+ value="GPT-4o-mini",
146
+ label="LLM ์—”์ง„ ์„ ํƒ"
147
+ )
148
+
149
+ chatbot = gr.Chatbot()
150
+ msg = gr.Textbox(label="๋ฉ”์‹œ์ง€ ์ž…๋ ฅ")
151
+ clear = gr.ClearButton()
152
+
153
+ msg.submit(chat, [msg, chatbot, llm_selector], [chatbot, chatbot])
154
+ msg.submit(lambda: "", None, msg)
155
+
156
+ app.launch()