suwesh commited on
Commit
9f85689
Β·
verified Β·
1 Parent(s): 50cdf35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -30
app.py CHANGED
@@ -1,26 +1,14 @@
1
  import gradio as gr
 
2
  from transformers import pipeline
3
  from gtts import gTTS
4
  import io
5
  import tempfile
6
- import speech_recognition as sr
7
- recognizer = sr.Recognizer()
8
-
9
- def speech_to_text(audio_in):
10
- with sr.AudioFile(audio_in) as source:
11
- audio = recognizer.record(source)
12
- try:
13
- text = recognizer.recognize_google(audio)
14
- return text
15
- except Exception as e:
16
- return "Sorry, I could not understand the audio :("
17
- except Exception as e:
18
- return "Sorry, my speech service is down :("
19
 
20
  """
21
  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
22
  """
23
- #client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
24
 
25
  modelpath = "distilgpt2"
26
 
@@ -51,24 +39,44 @@ def continue_conversation(new_message):
51
  #new_message = "Sure, I can help with that. Could you please provide me with some details about the loan you need?"
52
  #continue_conversation(new_message)
53
 
54
- def converse(audio_in, system_message, max_tokens, temperature, top_p):
55
- input_transcript = speech_to_text(inputsf)
56
- if input_transcript.startswith("Sorry"):
57
- return input_transcript
58
- else:
59
- history = []
60
- llm_out = pipe(input_transcript, max_length=256, num_return_sequences=1)
61
- print(llm_out)
62
- return llm_out
63
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  """
65
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
66
  """
67
- demo = gr.Interface(
68
- fn=converse,
69
- inputs=[
70
- gr.Microphone(label="Speak now...", type="numpy"),
71
- ],
72
  additional_inputs=[
73
  gr.Textbox(value="You are a Roleplaying Customer to a Housing Finance and Loans Company. DONOT say anything that sounds like an AI. Generate text like a natural human would.", label="System message"),
74
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
@@ -81,7 +89,6 @@ demo = gr.Interface(
81
  label="Top-p (nucleus sampling)",
82
  ),
83
  ],
84
- outputs="text",
85
  )
86
 
87
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
  from transformers import pipeline
4
  from gtts import gTTS
5
  import io
6
  import tempfile
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  """
9
  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
10
  """
11
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
12
 
13
  modelpath = "distilgpt2"
14
 
 
39
  #new_message = "Sure, I can help with that. Could you please provide me with some details about the loan you need?"
40
  #continue_conversation(new_message)
41
 
42
+ def respond(
43
+ message,
44
+ history: list[tuple[str, str]],
45
+ system_message,
46
+ max_tokens,
47
+ temperature,
48
+ top_p,
49
+ ):
50
+ messages = [{"role": "system", "content": system_message}]
51
+
52
+ for val in history:
53
+ if val[0]:
54
+ messages.append({"role": "user", "content": val[0]})
55
+ if val[1]:
56
+ messages.append({"role": "assistant", "content": val[1]})
57
+
58
+ messages.append({"role": "user", "content": message})
59
+
60
+ response = ""
61
+
62
+ for message in client.chat_completion(
63
+ messages,
64
+ max_tokens=max_tokens,
65
+ stream=True,
66
+ temperature=temperature,
67
+ top_p=top_p,
68
+ ):
69
+ token = message.choices[0].delta.content
70
+
71
+ response += token
72
+ yield response
73
+
74
+
75
  """
76
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
77
  """
78
+ demo = gr.ChatInterface(
79
+ respond,
 
 
 
80
  additional_inputs=[
81
  gr.Textbox(value="You are a Roleplaying Customer to a Housing Finance and Loans Company. DONOT say anything that sounds like an AI. Generate text like a natural human would.", label="System message"),
82
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
89
  label="Top-p (nucleus sampling)",
90
  ),
91
  ],
 
92
  )
93
 
94