Update fake.py
Browse files
fake.py
CHANGED
|
@@ -1,15 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import transformers_gradio
|
| 4 |
|
| 5 |
-
|
| 6 |
-
demo = gr.load(name="unsloth/gemma-3-1b-it-GGUF", src=transformers_gradio.registry)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
if __name__ == "__main__":
|
| 15 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
|
|
|
| 3 |
|
| 4 |
+
LLAMA_URL = "http://localhost:8000/completion"
|
|
|
|
| 5 |
|
| 6 |
+
# 🔎 Wikipedia (دائمًا)
|
| 7 |
+
def search_wikipedia(query):
|
| 8 |
+
try:
|
| 9 |
+
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query.replace(' ', '_')}"
|
| 10 |
+
res = requests.get(url, timeout=5)
|
| 11 |
+
|
| 12 |
+
if res.status_code == 200:
|
| 13 |
+
data = res.json()
|
| 14 |
+
return data.get("extract", "")[:1000]
|
| 15 |
+
except:
|
| 16 |
+
pass
|
| 17 |
+
|
| 18 |
+
return "No Wikipedia information found."
|
| 19 |
|
| 20 |
+
# 💬 الدردشة
|
| 21 |
+
def chat(message, history):
|
| 22 |
|
| 23 |
+
# 🔎 دائمًا نجلب من Wikipedia
|
| 24 |
+
wiki_text = search_wikipedia(message)
|
| 25 |
+
|
| 26 |
+
# 🧠 Prompt
|
| 27 |
+
prompt = f"""
|
| 28 |
+
You are a helpful assistant.
|
| 29 |
+
|
| 30 |
+
Use the Wikipedia information below to answer accurately.
|
| 31 |
+
|
| 32 |
+
Wikipedia:
|
| 33 |
+
{wiki_text}
|
| 34 |
+
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
# إضافة المحادثة السابقة
|
| 38 |
+
for h in history:
|
| 39 |
+
prompt += f"User: {h[0]}\nAssistant: {h[1]}\n"
|
| 40 |
+
|
| 41 |
+
prompt += f"User: {message}\nAssistant:"
|
| 42 |
+
|
| 43 |
+
# 🔗 llama.cpp
|
| 44 |
+
response = requests.post(LLAMA_URL, json={
|
| 45 |
+
"prompt": prompt,
|
| 46 |
+
"temperature": 0.7,
|
| 47 |
+
"top_p": 0.9,
|
| 48 |
+
"n_predict": 256,
|
| 49 |
+
"stop": ["User:"]
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
return response.json()["content"]
|
| 53 |
+
|
| 54 |
+
demo = gr.ChatInterface(chat)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
| 57 |
demo.launch()
|