Update app.py
Browse files
app.py
CHANGED
|
@@ -4,16 +4,25 @@ import os
|
|
| 4 |
import requests
|
| 5 |
import base64
|
| 6 |
import urllib.parse
|
|
|
|
| 7 |
|
| 8 |
-
# 🔱 CONFIGURATION
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
-
|
| 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
|
| 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 |
-
|
| 29 |
-
except Exception:
|
| 30 |
-
return "⚠️ Connection Error."
|
| 31 |
|
| 32 |
-
# 🔱 TEXT CHAT
|
| 33 |
else:
|
| 34 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
system_instruction = (
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"
|
| 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.
|
| 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")
|