Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,4 @@
|
|
1 |
-
import time
|
2 |
-
import os
|
3 |
-
import multiprocessing
|
4 |
-
import torch
|
5 |
-
import requests
|
6 |
-
import asyncio
|
7 |
-
import json
|
8 |
-
import aiohttp
|
9 |
from minivectordb.embedding_model import EmbeddingModel
|
10 |
from minivectordb.vector_database import VectorDatabase
|
11 |
from text_util_en_pt.cleaner import structurize_text, detect_language, Language
|
@@ -31,17 +24,17 @@ def index_and_search(query, text):
|
|
31 |
|
32 |
# Indexing
|
33 |
vector_db = VectorDatabase()
|
34 |
-
sentences = [s['sentence'] for s in structurize_text(text)]
|
35 |
|
36 |
for idx, sentence in enumerate(sentences):
|
37 |
sentence_embedding = model.extract_embeddings(sentence)
|
38 |
vector_db.store_embedding(idx + 1, sentence_embedding, {'sentence': sentence})
|
39 |
-
|
40 |
embedding_time = time.time() - start
|
41 |
|
42 |
# Retrieval
|
43 |
start = time.time()
|
44 |
-
search_results = vector_db.find_most_similar(query_embedding, k=30)
|
45 |
retrieval_time = time.time() - start
|
46 |
return '\n'.join([s['sentence'] for s in search_results[2]]), embedding_time, retrieval_time
|
47 |
|
@@ -52,22 +45,18 @@ def generate_search_terms(message, lang):
|
|
52 |
prompt = f"From the following text, generate some search terms: \"{message}\"\nYour answer should be just the most appropriate search term, and nothing else."
|
53 |
|
54 |
url = "https://openrouter.ai/api/v1/chat/completions"
|
55 |
-
headers = {
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
"messages": [
|
68 |
-
{"role": "user", "content": prompt}
|
69 |
-
]
|
70 |
-
}
|
71 |
|
72 |
response = requests.post(url, headers=headers, json=body)
|
73 |
return response.json()['choices'][0]['message']['content']
|
@@ -131,22 +120,18 @@ async def predict(message, history):
|
|
131 |
full_response += "\nResponse: "
|
132 |
|
133 |
url = "https://openrouter.ai/api/v1/chat/completions"
|
134 |
-
headers = {
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
"messages": [
|
147 |
-
{"role": "user", "content": prompt}
|
148 |
-
]
|
149 |
-
}
|
150 |
|
151 |
async with aiohttp.ClientSession() as session:
|
152 |
async with session.post(url, headers=headers, json=body) as response:
|
@@ -173,26 +158,15 @@ async def predict(message, history):
|
|
173 |
except Exception:
|
174 |
pass
|
175 |
|
176 |
-
# Define a custom theme with colors similar to the provided image
|
177 |
-
custom_theme = gr.themes.Default(
|
178 |
-
primary_hue="purple", # Custom primary color (similar to the purple in the image)
|
179 |
-
secondary_hue="indigo", # Custom secondary color
|
180 |
-
font="Roboto", # Custom font
|
181 |
-
rounded_corners=True, # Enable rounded corners
|
182 |
-
background_color="#1a1a2e", # Custom dark background color
|
183 |
-
text_color="#f0f0f0" # Custom text color for better contrast
|
184 |
-
)
|
185 |
-
|
186 |
-
# Apply the custom theme to the Gradio interface
|
187 |
gr.ChatInterface(
|
188 |
predict,
|
189 |
-
title="
|
|
|
190 |
retry_btn=None,
|
191 |
undo_btn=None,
|
192 |
examples=[
|
193 |
'What is the current sentiment of the Brazil election?',
|
194 |
'Compare the current economies of China and India?',
|
195 |
-
'What are new shoe design trends in 2024
|
196 |
-
]
|
197 |
-
|
198 |
-
).launch()
|
|
|
1 |
+
import time, os, multiprocessing, torch, requests, asyncio, json, aiohttp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from minivectordb.embedding_model import EmbeddingModel
|
3 |
from minivectordb.vector_database import VectorDatabase
|
4 |
from text_util_en_pt.cleaner import structurize_text, detect_language, Language
|
|
|
24 |
|
25 |
# Indexing
|
26 |
vector_db = VectorDatabase()
|
27 |
+
sentences = [ s['sentence'] for s in structurize_text(text)]
|
28 |
|
29 |
for idx, sentence in enumerate(sentences):
|
30 |
sentence_embedding = model.extract_embeddings(sentence)
|
31 |
vector_db.store_embedding(idx + 1, sentence_embedding, {'sentence': sentence})
|
32 |
+
|
33 |
embedding_time = time.time() - start
|
34 |
|
35 |
# Retrieval
|
36 |
start = time.time()
|
37 |
+
search_results = vector_db.find_most_similar(query_embedding, k = 30)
|
38 |
retrieval_time = time.time() - start
|
39 |
return '\n'.join([s['sentence'] for s in search_results[2]]), embedding_time, retrieval_time
|
40 |
|
|
|
45 |
prompt = f"From the following text, generate some search terms: \"{message}\"\nYour answer should be just the most appropriate search term, and nothing else."
|
46 |
|
47 |
url = "https://openrouter.ai/api/v1/chat/completions"
|
48 |
+
headers = { "Content-Type": "application/json",
|
49 |
+
"Authorization": f"Bearer {openrouter_key}" }
|
50 |
+
body = { "stream": False,
|
51 |
+
"models": [
|
52 |
+
"mistralai/mistral-7b-instruct:free",
|
53 |
+
"openchat/openchat-7b:free"
|
54 |
+
],
|
55 |
+
"route": "fallback",
|
56 |
+
"max_tokens": 1024,
|
57 |
+
"messages": [
|
58 |
+
{"role": "user", "content": prompt}
|
59 |
+
] }
|
|
|
|
|
|
|
|
|
60 |
|
61 |
response = requests.post(url, headers=headers, json=body)
|
62 |
return response.json()['choices'][0]['message']['content']
|
|
|
120 |
full_response += "\nResponse: "
|
121 |
|
122 |
url = "https://openrouter.ai/api/v1/chat/completions"
|
123 |
+
headers = { "Content-Type": "application/json",
|
124 |
+
"Authorization": f"Bearer {openrouter_key}" }
|
125 |
+
body = { "stream": True,
|
126 |
+
"models": [
|
127 |
+
"mistralai/mistral-7b-instruct:free",
|
128 |
+
"openchat/openchat-7b:free"
|
129 |
+
],
|
130 |
+
"route": "fallback",
|
131 |
+
"max_tokens": 1024,
|
132 |
+
"messages": [
|
133 |
+
{"role": "user", "content": prompt}
|
134 |
+
] }
|
|
|
|
|
|
|
|
|
135 |
|
136 |
async with aiohttp.ClientSession() as session:
|
137 |
async with session.post(url, headers=headers, json=body) as response:
|
|
|
158 |
except Exception:
|
159 |
pass
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
gr.ChatInterface(
|
162 |
predict,
|
163 |
+
title="Live Web Chat",
|
164 |
+
description="",
|
165 |
retry_btn=None,
|
166 |
undo_btn=None,
|
167 |
examples=[
|
168 |
'What is the current sentiment of the Brazil election?',
|
169 |
'Compare the current economies of China and India?',
|
170 |
+
'What are new shoe design trends in 2024',
|
171 |
+
]
|
172 |
+
).launch()
|
|