Maryaa4 commited on
Commit
8966170
·
verified ·
1 Parent(s): ee9e6aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -73
app.py CHANGED
@@ -1,87 +1,23 @@
1
- import os
2
- import requests
3
  import gradio as gr
4
- from fastapi import FastAPI, Request
5
  from transformers import pipeline
6
 
7
- # ======================
8
- # 1) تحميل المودل من الريبو حقك
9
- # ======================
10
-
11
  deployed_repo_id = "maryaa4/my-arabic-sentiment-model"
12
- loaded_sentiment_pipeline = pipeline(
13
- "sentiment-analysis",
14
- model=deployed_repo_id,
15
- trust_remote_code=True,
16
- )
17
-
18
- def predict_sentiment(text: str) -> str:
19
- if not text or text.strip() == "":
20
- return "رجاءً أدخل نص عربي عشان أقدر أحلله 📝"
21
 
 
 
 
22
  result = loaded_sentiment_pipeline(text)
23
- label = result[0]["label"]
24
- score = result[0]["score"]
25
-
26
- # تقدرين تغيرين طريقة العرض هنا
27
- return f"التصنيف: {label}\nنسبة الثقة: {score:.2f}"
28
-
29
- # ======================
30
- # 2) واجهة Gradio للسبيس
31
- # ======================
32
 
33
  iface = gr.Interface(
34
  fn=predict_sentiment,
35
  inputs=gr.Textbox(lines=5, placeholder="أدخل النص العربي هنا..."),
36
  outputs="text",
37
  title="Arabic Sentiment Analysis",
38
- description="موديل لتحليل المشاعر في النصوص العربية باستخدام النموذج: maryaa4/my-arabic-sentiment-model",
39
  )
40
 
41
- # ======================
42
- # 3) إعداد FastAPI + Telegram Webhook
43
- # ======================
44
-
45
- app = FastAPI()
46
-
47
- # نحط التوكن في Secrets في السبيس باسم TELEGRAM_TOKEN
48
- BOT_TOKEN = os.getenv("TELEGRAM_TOKEN")
49
- TELEGRAM_API = (
50
- f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
51
- if BOT_TOKEN
52
- else None
53
- )
54
-
55
- @app.post("/telegram")
56
- async def telegram_webhook(request: Request):
57
- """
58
- هذا هو الويبهوك اللي تيليجرام يناديه
59
- """
60
- data = await request.json()
61
-
62
- # نتأكد إن فيه رسالة
63
- if "message" in data and "text" in data["message"]:
64
- chat_id = data["message"]["chat"]["id"]
65
- user_text = data["message"]["text"]
66
-
67
- # نستخدم نفس الدالة حقت Gradio
68
- reply = predict_sentiment(user_text)
69
-
70
- if TELEGRAM_API:
71
- try:
72
- requests.post(
73
- TELEGRAM_API,
74
- json={"chat_id": chat_id, "text": reply},
75
- )
76
- except Exception as e:
77
- print("Error sending message to Telegram:", e)
78
-
79
- return {"ok": True}
80
-
81
- # ======================
82
- # 4) تركيب Gradio فوق FastAPI
83
- # ======================
84
-
85
- # هذا يخلي السبيس يفتح واجهة Gradio على المسار /
86
- # والـ API حق تيليجرام على /telegram
87
- app = gr.mount_gradio_app(app, iface, path="/")
 
 
 
1
  import gradio as gr
 
2
  from transformers import pipeline
3
 
 
 
 
 
4
  deployed_repo_id = "maryaa4/my-arabic-sentiment-model"
5
+ loaded_sentiment_pipeline = pipeline('sentiment-analysis', model=deployed_repo_id, trust_remote_code=True)
 
 
 
 
 
 
 
 
6
 
7
+ def predict_sentiment(text):
8
+ if not text:
9
+ return "Please enter some text for sentiment analysis."
10
  result = loaded_sentiment_pipeline(text)
11
+ label = result[0]['label']
12
+ score = result[0]['score']
13
+ return f"Sentiment: {label.capitalize()}, Confidence: {score:.4f}"
 
 
 
 
 
 
14
 
15
  iface = gr.Interface(
16
  fn=predict_sentiment,
17
  inputs=gr.Textbox(lines=5, placeholder="أدخل النص العربي هنا..."),
18
  outputs="text",
19
  title="Arabic Sentiment Analysis",
20
+ description=f"A sentiment analysis model for Arabic text, deployed from maryaa4/my-arabic-sentiment-model. Enter Arabic text and get the predicted sentiment."
21
  )
22
 
23
+ iface.launch(share=True) # share=True for a temporary public link