huggingfacecospacesYOURNAMEapp commited on
Commit
a09e7ab
ยท
verified ยท
1 Parent(s): 7f3d8d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -1,23 +1,20 @@
1
  import gradio as gr
2
  import requests
3
- import json
4
  import time
5
 
6
  def monster_chat(message, history):
7
- # ืฉื™ืžื•ืฉ ื‘ืžื•ื“ืœ Llama-3-8B - ื”ื›ื™ ื™ืฆื™ื‘ ื•ืžื”ื™ืจ ื‘-API
8
  API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
9
 
10
- # ื‘ื ื™ื™ืช ื”ืคืจื•ืžืคื˜ ื‘ืฆื•ืจื” ืฉื”ืžื•ื“ืœ ื™ื‘ื™ืŸ ืฉื”ื•ื ื‘ืฆ'ืื˜
11
  payload = {
12
- "inputs": f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nYou are a funny and charismatic monster AI. Answer in Hebrew only.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n",
13
- "parameters": {"max_new_tokens": 200, "temperature": 0.7, "return_full_text": False}
14
  }
15
 
16
- # ื ื™ืกื™ื•ืŸ ืจืืฉื•ืŸ ืœืฉืœื•ื— ื”ื•ื“ืขื”
17
  try:
18
  response = requests.post(API_URL, json=payload, timeout=15)
19
 
20
- # ืื ื”ืžื•ื“ืœ ื‘ื˜ืขื™ื ื”, ื ื—ื›ื” 5 ืฉื ื™ื•ืช ื•ื ื ืกื” ืฉื•ื‘ ืื•ื˜ื•ืžื˜ื™ืช
21
  if response.status_code == 503:
22
  time.sleep(5)
23
  response = requests.post(API_URL, json=payload, timeout=15)
@@ -25,17 +22,15 @@ def monster_chat(message, history):
25
  result = response.json()
26
 
27
  if isinstance(result, list) and len(result) > 0:
28
- return result[0].get('generated_text', "ื•ื•ืืœื”, ืื™ืŸ ืœื™ ืžื™ืœื™ื!")
29
- elif isinstance(result, dict) and "generated_text" in result:
30
- return result["generated_text"]
31
- else:
32
- return "ื”ืžืคืœืฆืช ืžื ืกื” ืœื”ื‘ื™ืŸ ืžื” ืืžืจืช... ืชืฉืœื— ืฉื•ื‘!"
33
- except Exception as e:
34
- return "ื™ืฉ ืชืงืœื” ื‘ื—ื™ื‘ื•ืจ ืœืžืคืœืฆืช, ื ืกื” ืฉื•ื‘ ื‘ืขื•ื“ ื›ืžื” ืฉื ื™ื•ืช."
35
 
36
- # ืขื™ืฆื•ื‘ ื”ืžืžืฉืง
37
- with gr.Blocks(theme=gr.themes.Default(primary_hue="purple", secondary_hue="indigo")) as demo:
38
- gr.HTML("<div style='text-align:center'><h1>๐Ÿ‘พ THE REAL MONSTER AI</h1><p>ืขื›ืฉื™ื• ืขื ืžื ื•ืข Llama-3 ืขื•ืฆืžืชื™</p></div>")
39
- gr.ChatInterface(fn=monster_chat, type="messages")
40
 
 
41
  demo.launch()
 
1
  import gradio as gr
2
  import requests
 
3
  import time
4
 
5
  def monster_chat(message, history):
6
+ # ืžื•ื“ืœ Llama-3 ื”ื™ืฆื™ื‘
7
  API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
8
 
 
9
  payload = {
10
+ "inputs": f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nYou are a funny AI. Answer in Hebrew.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n",
11
+ "parameters": {"max_new_tokens": 150, "temperature": 0.7}
12
  }
13
 
 
14
  try:
15
  response = requests.post(API_URL, json=payload, timeout=15)
16
 
17
+ # ืื ื”ืžื•ื“ืœ ื‘ื˜ืขื™ื ื” (503), ื ื—ื›ื” ืงืฆืช
18
  if response.status_code == 503:
19
  time.sleep(5)
20
  response = requests.post(API_URL, json=payload, timeout=15)
 
22
  result = response.json()
23
 
24
  if isinstance(result, list) and len(result) > 0:
25
+ return result[0].get('generated_text', "ื”ืžืคืœืฆืช ืื™ื‘ื“ื” ืืช ื”ืงื•ืœ!")
26
+ return "ื”ืžืคืœืฆืช ื—ื•ืฉื‘ืช... ื ืกื” ืœืฉืœื•ื— ืฉื•ื‘."
27
+ except:
28
+ return "ืชืงืœื” ื‘ื—ื™ื‘ื•ืจ ืœืžืคืœืฆืช."
 
 
 
29
 
30
+ # ื™ืฆื™ืจืช ื”ืžืžืฉืง ื‘ืฆื•ืจื” ื ืงื™ื™ื” ื‘ืœื™ ืคืจืžื˜ืจื™ื ืฉื‘ื•ืจื™ื
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("# ๐Ÿ‘พ MONSTER CHAT")
33
+ gr.ChatInterface(fn=monster_chat)
34
 
35
+ # ื”ืขื‘ืจื ื• ืืช ื”-theme ืœืคื”, ื‘ื“ื™ื•ืง ื›ืžื• ืฉื”ืฉื’ื™ืื” ื‘ื™ืงืฉื”
36
  demo.launch()