DKethan commited on
Commit
c37c54b
·
verified ·
1 Parent(s): 5126292

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -32
app.py CHANGED
@@ -2,49 +2,29 @@ import gradio as gr
2
  from transformers import pipeline
3
 
4
  # Load the text generation pipeline
5
- pipe = pipeline("text-generation", model="microsoft/Phi-3.5-mini-instruct", trust_remote_code=True)
6
 
7
  # Define the function for text generation
8
- def generate(user_input):
9
- try:
10
- # System prompt and user input
11
- system_prompt = "You are a text generator. Your task is to generate text that is requested by the user. Also make sure this looks simple."
12
- messages = [
13
- {"role": "system", "content": system_prompt},
14
- {"role": "user", "content": user_input}
15
- ]
16
- # Generate a response using the pipeline
17
- result = pipe(messages, max_length=100, num_return_sequences=1)
18
- # Format the response
19
- if result and isinstance(result, list) and 'generated_text' in result[0]:
20
- response_text = result[0]['generated_text']
21
- return [
22
- {"role": "system", "content": system_prompt},
23
- {"role": "user", "content": user_input},
24
- {"role": "assistant", "content": response_text}
25
- ]
26
- else:
27
- return [{"role": "error", "content": "Error: No valid response generated."}]
28
- except Exception as e:
29
- return [{"role": "error", "content": f"Error: {e}"}]
30
 
31
  # Define examples
32
  examples = [
33
- ["What is the capital of France?"],
34
- ["Generate a poem about nature."],
35
- ["Write a story about a brave knight."],
36
- ["Explain the concept of reinforcement learning."]
 
37
  ]
38
 
39
  # Create the Gradio interface
40
  run = gr.Interface(
41
  fn=generate,
42
  inputs=gr.Textbox(lines=5, label="Input Text"),
43
- outputs=gr.JSON(label="Generated Conversation"),
44
- examples=examples,
45
- cache_examples=True
46
  )
47
 
48
  # Launch the app
49
- if __name__ == "__main__":
50
- run.launch()
 
2
  from transformers import pipeline
3
 
4
  # Load the text generation pipeline
5
+ pipe = pipeline("text-generation", model="microsoft/phi-4", trust_remote_code=True)
6
 
7
  # Define the function for text generation
8
+ def generate(text):
9
+ result = generator(text, max_length=100, num_return_sequences=1)
10
+ return result[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  # Define examples
13
  examples = [
14
+ ["What is the fundamental difference between supervised and unsupervised learning"],
15
+ ["What is overfitting in supervised learning"],
16
+ ["What is a convolutional neural network"],
17
+ ["Describe the concept of transfer learning and its significance in deep learning"]
18
+
19
  ]
20
 
21
  # Create the Gradio interface
22
  run = gr.Interface(
23
  fn=generate,
24
  inputs=gr.Textbox(lines=5, label="Input Text"),
25
+ outputs=gr.Textbox(label="Generated Text"),
26
+ examples=examples
 
27
  )
28
 
29
  # Launch the app
30
+ run.launch()