minzo456 commited on
Commit
8d21d29
·
verified ·
1 Parent(s): b7ef2b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -4,16 +4,25 @@ import os
4
  import requests
5
  import base64
6
  import urllib.parse
 
7
 
8
- # 🔱 CONFIGURATION (NATURAL SINHALA ENGINE)
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
- # සිංහල භාෂාව සඳහා දැනට ඇති හොඳම Open-source මොළ
11
- TEXT_MODEL = "meta-llama/Meta-Llama-3.1-8B-Instruct"
12
 
13
  client = InferenceClient(token=HF_TOKEN)
14
 
 
 
 
 
 
 
 
 
 
15
  def dual_engine(message, history):
16
- # 🔱 IMAGE GENERATION (POLLINATIONS FLUX)
17
  if message.lower().startswith("/image "):
18
  prompt = message.replace("/image ", "").strip()
19
  safe_prompt = urllib.parse.quote(prompt)
@@ -24,18 +33,22 @@ def dual_engine(message, history):
24
  if response.status_code == 200:
25
  img_str = base64.b64encode(response.content).decode()
26
  return f"IMAGE_DATA:data:image/png;base64,{img_str}"
27
- else:
28
- return "⚠️ Image Engine Busy. Please retry."
29
- except Exception:
30
- return "⚠️ Connection Error."
31
 
32
- # 🔱 TEXT CHAT (LLAMA 3.1 - NATURAL SINHALA)
33
  else:
34
- # සිංහෙන් වඩාත් ඳින් පිළිතුරු දීමට System Prompt එ සැකසීම
 
 
 
 
 
 
35
  system_instruction = (
36
- "You are MINZO AI, created by MINZO-PRIME. "
37
- "You must communicate in natural, fluent Sinhala. "
38
- "Helpful, brilliant, and strategic in your responses. "
39
  "පින්තූර සෑදීමට /image විධානය භාවිතා කරන්න."
40
  )
41
 
@@ -50,11 +63,11 @@ def dual_engine(message, history):
50
  messages,
51
  model=TEXT_MODEL,
52
  max_tokens=1500,
53
- temperature=0.7 # සිංහල ගලායාම ස්වාභාවික කිරීමට
54
  )
55
  return response.choices[0].message.content
56
- except Exception:
57
- return "⚠️ පද්ධතියේ දෝෂයක්. කරුණාකර නැවත උත්සාහ කරන්න."
58
 
59
  # Gradio Interface
60
  demo = gr.ChatInterface(fn=dual_engine, api_name="chat")
 
4
  import requests
5
  import base64
6
  import urllib.parse
7
+ from duckduckgo_search import DDGS # Web Search සඳහා
8
 
9
+ # 🔱 CONFIGURATION
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
+ TEXT_MODEL = "google/gemma-2-27b-it" # Gemma Core 4 (ප්‍රබල සංස්කරණ)
 
12
 
13
  client = InferenceClient(token=HF_TOKEN)
14
 
15
+ def web_search(query):
16
+ """අන්තර්ජාලයෙන් අලුත්ම තොරතුරු සෙවීමේ මෙවලම"""
17
+ try:
18
+ results = DDGS().text(query, max_results=3)
19
+ search_data = "\n".join([f"Source: {r['body']}" for r in results])
20
+ return search_data
21
+ except Exception:
22
+ return "No real-time data found."
23
+
24
  def dual_engine(message, history):
25
+ # 🔱 IMAGE GENERATION ENGINE
26
  if message.lower().startswith("/image "):
27
  prompt = message.replace("/image ", "").strip()
28
  safe_prompt = urllib.parse.quote(prompt)
 
33
  if response.status_code == 200:
34
  img_str = base64.b64encode(response.content).decode()
35
  return f"IMAGE_DATA:data:image/png;base64,{img_str}"
36
+ else: return "⚠️ Image Engine Busy."
37
+ except Exception: return "⚠️ Connection Error."
 
 
38
 
39
+ # 🔱 TEXT CHAT WITH WEB SEARCH
40
  else:
41
+ # ත් තුරු අවශ්‍ය ැයි තරණයිරීම (Simple Keyword Check)
42
+ search_keywords = ["news", "today", "weather", "latest", "price", "අද", "අලුත්ම"]
43
+ context = ""
44
+
45
+ if any(word in message.lower() for word in search_keywords):
46
+ context = web_search(message)
47
+
48
  system_instruction = (
49
+ f"නම: MINZO AI. නිර්මාතෘ: MINZO-PRIME. "
50
+ f"ඔබ සතු අන්තර්ජාල සෙවුම් දත්ත: {context}\n"
51
+ "ඉහත දත්ත පාවිච්චි කරමින් ඉතාමත් නිවැරදි සහ ස්වාභාවික සිංහලෙන් පිළිතුරු දෙන්න. "
52
  "පින්තූර සෑදීමට /image විධානය භාවිතා කරන්න."
53
  )
54
 
 
63
  messages,
64
  model=TEXT_MODEL,
65
  max_tokens=1500,
66
+ temperature=0.6
67
  )
68
  return response.choices[0].message.content
69
+ except Exception as e:
70
+ return f"⚠️ Neural Link Error: {str(e)}"
71
 
72
  # Gradio Interface
73
  demo = gr.ChatInterface(fn=dual_engine, api_name="chat")