Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# Translation Pipelines
|
| 6 |
+
translator_ur_en = pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en")
|
| 7 |
+
translator_en_ur = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ur")
|
| 8 |
+
|
| 9 |
+
# Hugging Face API Setup
|
| 10 |
+
HF_API_TOKEN = "YOUR_HF_API_KEY" # Replace with your token from https://huggingface.co/settings/tokens
|
| 11 |
+
DEEPSEEK_MODEL = "deepseek-ai/deepseek-llm-7b-chat"
|
| 12 |
+
|
| 13 |
+
headers = {
|
| 14 |
+
"Authorization": f"Bearer {HF_API_TOKEN}",
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
def query_deepseek(prompt):
|
| 19 |
+
payload = {
|
| 20 |
+
"inputs": f"<|system|>You are a helpful assistant.<|user|>{prompt}<|assistant|>",
|
| 21 |
+
"parameters": {"max_new_tokens": 200}
|
| 22 |
+
}
|
| 23 |
+
response = requests.post(
|
| 24 |
+
f"https://api-inference.huggingface.co/models/{DEEPSEEK_MODEL}",
|
| 25 |
+
headers=headers,
|
| 26 |
+
json=payload
|
| 27 |
+
)
|
| 28 |
+
result = response.json()
|
| 29 |
+
return result[0]["generated_text"].split("<|assistant|>")[-1].strip()
|
| 30 |
+
|
| 31 |
+
# Streamlit UI
|
| 32 |
+
st.title("🤖 Urdu AI Assistant (Powered by DeepSeek)")
|
| 33 |
+
|
| 34 |
+
urdu_input = st.text_area("اپنا سوال اردو میں لکھیں:", height=100)
|
| 35 |
+
|
| 36 |
+
if st.button("جواب حاصل کریں"):
|
| 37 |
+
with st.spinner("ترجمہ ہو رہا ہے..."):
|
| 38 |
+
english_prompt = translator_ur_en(urdu_input)[0]['translation_text']
|
| 39 |
+
|
| 40 |
+
with st.spinner("DeepSeek سے جواب حاصل کیا جا رہا ہے..."):
|
| 41 |
+
english_response = query_deepseek(english_prompt)
|
| 42 |
+
|
| 43 |
+
with st.spinner("جواب اردو میں ترجمہ ہو رہا ہے..."):
|
| 44 |
+
urdu_response = translator_en_ur(english_response)[0]['translation_text']
|
| 45 |
+
|
| 46 |
+
st.markdown("### 📝 جواب:")
|
| 47 |
+
st.write(urdu_response)
|