Spaces:
Build error
Build error
Update app.py
Browse files14 unmodified lines1516171819202122232425266 unmodified lines3334353637383994 unmodified lines13413513613713813914014114267 unmodified lines21021121221321421521614 unmodified linesfrom gensim.models import Word2Vecfrom typing import List, Optional, Tupleimport gradio as grlogger = logging.getLogger(__name__)logging.basicConfig(level=logging.INFO)api_key = os.getenv("MISTRAL_API_KEY")client = Mistral(api_key=api_key)def get_text_embedding_with_rate_limit(text_list, initial_delay=2, max_retries=10, max_delay=60):embeddings = []6 unmodified linesif token_count > 16384:logger.warning("Text chunk exceeds the token limit. Truncating the text.")text = " ".join(text.split()[:16384])response = client.embeddings.create(model="mistral-embed", inputs=[text])embeddings.extend([embedding.embedding for embedding in response.data])time.sleep(delay)break94 unmodified lineslogging.info("Word2Vec model trained.")return modelasync def get_text_embedding(self, text: str, model: str = "mistral-embed") -> np.ndarray:try:response = await client.embeddings.create_async(model=model, inputs=[text])return np.array(response.data[0].embedding)except Exception as e:logging.error(f"Error fetching embedding: {e}")67 unmodified linescontext = "\n\n".join([doc['text'] for doc in reranked_docs[:5]])prompt = self.build_prompt(context, user_query, response_style)try:async_response = await client.chat.stream_async(model=model, messages=[{"role": "user", "content": prompt}])response = ""async for chunk in async_response:response += chunk.data.choices[0].delta.content14 unmodified lines15161718192021222324252627286 unmodified lines3536373839404194 unmodified lines13613713813914014114214314467 unmodified lines21221321421521621721821922022122214 unmodified linesfrom gensim.models import Word2Vecfrom typing import List, Optional, Tupleimport gradio as grimport openailogger = logging.getLogger(__name__)logging.basicConfig(level=logging.INFO)api_key = os.getenv("MISTRAL_API_KEY")client = Mistral(api_key=api_key)openai.api_key = os.getenv("OPENAI_API_KEY")def get_text_embedding_with_rate_limit(text_list, initial_delay=2, max_retries=10, max_delay=60):embeddings = []6 unmodified linesif token_count > 16384:logger.warning("Text chunk exceeds the token limit. Truncating the text.")text = " ".join(text.split()[:16384])response = openai.embeddings.create(model="text-embedding-3-small", input=[text])embeddings.extend([embedding.embedding for embedding in response.data])time.sleep(delay)break94 unmodified lineslogging.info("Word2Vec model trained.")return modelasync def get_text_embedding(self, text: str, model: str = "text-embedding-3-small") -> np.ndarray:try:response = await openai.embeddings.create(model="text-embedding-3-small", input=[text])return np.array(response.data[0].embedding)except Exception as e:logging.error(f"Error fetching embedding: {e}")67 unmodified linescontext = "\n\n".join([doc['text'] for doc in reranked_docs[:5]])prompt = self.build_prompt(context, user_query, response_style)try:async_response = await openai.chat.completions.acreate(model="gpt-4o-mini", # or "gpt-4o-mini" if available in your tiermessages=[{"role": "user", "content": prompt}],stream=True)response = ""async for chunk in async_response:response += chunk.data.choices[0].delta.content
app.py+10-4