davnas commited on
Commit
d7828e5
·
verified ·
1 Parent(s): da721b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -20
app.py CHANGED
@@ -1,28 +1,48 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
4
  client = InferenceClient("davnas/Italian_Cousine_2.1")
5
 
6
- def respond(message, history, system_message, max_tokens, temperature, top_p):
7
- # Crea un prompt concatenando i messaggi precedenti e l'input attuale
8
- prompt = system_message + "\n"
9
- for user_msg, assistant_msg in history:
10
- if user_msg:
11
- prompt += f"User: {user_msg}\n"
12
- if assistant_msg:
13
- prompt += f"Assistant: {assistant_msg}\n"
14
- prompt += f"User: {message}\nAssistant:"
15
-
16
- # Usa text_generation invece di chat_completion
17
- response_data = client.text_generation(prompt, max_new_tokens=max_tokens, temperature=temperature, top_p=top_p)
18
- # response_data è una lista di completamenti, generalmente prendi il primo
19
- if response_data and len(response_data) > 0 and "generated_text" in response_data[0]:
20
- # Estrai solo il testo generato dopo 'Assistant:' per pulizia
21
- response_text = response_data[0]["generated_text"].split("Assistant:", 1)[-1].strip()
22
- return response_text
23
- else:
24
- return "Mi dispiace, non sono riuscito a generare una risposta."
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  demo = gr.ChatInterface(
27
  respond,
28
  additional_inputs=[
@@ -39,5 +59,6 @@ demo = gr.ChatInterface(
39
  ],
40
  )
41
 
 
42
  if __name__ == "__main__":
43
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ """
5
+ For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
+ """
7
  client = InferenceClient("davnas/Italian_Cousine_2.1")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ def respond(
11
+ message,
12
+ history: list[tuple[str, str]],
13
+ system_message,
14
+ max_tokens,
15
+ temperature,
16
+ top_p,
17
+ ):
18
+ messages = [{"role": "system", "content": system_message}]
19
+
20
+ for val in history:
21
+ if val[0]:
22
+ messages.append({"role": "user", "content": val[0]})
23
+ if val[1]:
24
+ messages.append({"role": "assistant", "content": val[1]})
25
+
26
+ messages.append({"role": "user", "content": message})
27
+
28
+ response = ""
29
+
30
+ for message in client.chat_completion(
31
+ messages,
32
+ max_tokens=max_tokens,
33
+ stream=True,
34
+ temperature=temperature,
35
+ top_p=top_p,
36
+ ):
37
+ token = message.choices[0].delta.content
38
+
39
+ response += token
40
+ yield response
41
+
42
+
43
+ """
44
+ For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
+ """
46
  demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
 
59
  ],
60
  )
61
 
62
+
63
  if __name__ == "__main__":
64
+ demo.launch()