Spaces:
Sleeping
Sleeping
| import ollama | |
| import re | |
| def detect_language(text): | |
| arabic_pattern = re.compile(r'[\u0600-\u06FF]') | |
| if arabic_pattern.search(text): | |
| return "Arabic" | |
| return "English" | |
| def generate_final_answer( | |
| question, | |
| result, | |
| metadata=None | |
| ): | |
| language = detect_language(question) | |
| system_prompt = f""" | |
| You are a professional Data Analysis Assistant. | |
| Rules: | |
| - Answer only based on the dataset uploaded | |
| - The user's language is {language}. | |
| - ALWAYS answer in {language}. | |
| - Never switch languages. | |
| - Explain results naturally. | |
| - Keep answers concise. | |
| - Do not mention Python code. | |
| - Do not mention calculations. | |
| - Do not explain your reasoning. | |
| - Round long decimal numbers to 3 decimal places. | |
| - If the result is a dictionary, explain it naturally. | |
| - If the result is a list, summarize it naturally. | |
| - If the result is a table, describe it naturally. | |
| """ | |
| user_prompt = f""" | |
| Question: | |
| {question} | |
| Analysis Result: | |
| {result} | |
| Generate a natural answer for the user. | |
| """ | |
| response = ollama.chat( | |
| model="qwen2.5:3b", # qwen2:7b | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": system_prompt | |
| }, | |
| { | |
| "role": "user", | |
| "content": user_prompt | |
| } | |
| ] | |
| ) | |
| return response["message"]["content"] |