Rooni commited on
Commit
465d036
·
1 Parent(s): beafc79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -69
app.py CHANGED
@@ -1,76 +1,22 @@
1
  import gradio as gr
2
- import requests
3
- import json
4
- import os
5
- from PIL import Image
6
 
7
- def generate(description, image):
8
- if image is None:
9
- # No image uploaded, generate text only
10
- return generate_text(description)
11
 
12
- # Image uploaded, generate text with image caption
13
- return generate_text_with_image(description, image)
 
 
14
 
15
- def generate_text(description):
16
- headers = {
17
- 'Content-Type': 'application/json',
18
- 'Authorization': f'Bearer {os.getenv("API_KEY")}'
19
- }
20
 
21
- payload = {
22
- 'prompt': f'{description}',
23
- 'max_tokens': 10000,
24
- 'model': os.getenv("MODEL")
25
- }
26
 
27
- response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
28
- data = json.loads(response.text)
29
 
30
- if 'choices' in data and len(data['choices']) > 0:
31
- command = data['choices'][0]['text'].strip()
32
- return command
33
- elif 'error' in data:
34
- error_message = data['error']['message']
35
- return f'Ошибка: {error_message}'
36
- else:
37
- return f'Не удалось сгенерировать текст. {data}'
38
-
39
- def generate_text_with_image(description, image):
40
- # Preprocess image
41
- image = Image.open(image).resize((224, 224))
42
- image = image.convert('RGB')
43
- image = image.tobytes()
44
-
45
- # Convert image to base64 encoding
46
- image_base64 = base64.b64encode(image).decode('utf-8')
47
-
48
- headers = {
49
- 'Content-Type': 'application/json',
50
- 'Authorization': f'Bearer {os.getenv("API_KEY")}'
51
- }
52
-
53
- payload = {
54
- 'prompt': f'{description}',
55
- 'max_tokens': 10000,
56
- 'model': os.getenv("MODEL"),
57
- 'input_image': image_base64
58
- }
59
-
60
- response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
61
- data = json.loads(response.text)
62
-
63
- if 'choices' in data and len(data['choices']) > 0:
64
- command = data['choices'][0]['text'].strip()
65
- return command
66
- elif 'error' in data:
67
- error_message = data['error']['message']
68
- return f'Ошибка: {error_message}'
69
- else:
70
- return f'Не удалось сгенерировать текст. {data}'
71
-
72
- iface = gr.Interface(fn=generate, inputs=[
73
- gr.Textbox(label="Запрос"),
74
- gr.File(label="Изображение")
75
- ], outputs=gr.Textbox(label="Ответ"), title="GPT")
76
- iface.launch()
 
1
  import gradio as gr
2
+ import huggingface
 
 
 
3
 
4
+ # Загружаем модель
5
+ model = huggingface.transformers.AutoModelForQuestionAnswering.from_pretrained("facebook/bart-base")
 
 
6
 
7
+ # Функция для генерации ответа
8
+ def generate_answer(image, prompt):
9
+ # Получаем текст из изображения
10
+ text = huggingface.vision.ImageCaptioner.from_pretrained("facebook/bart-base").generate(image)
11
 
12
+ # Если был указан дополнительный prompt, добавляем его к тексту
13
+ if prompt:
14
+ text += f" {prompt}"
 
 
15
 
16
+ # Генерируем ответ на вопрос
17
+ answer = model.generate(text=text, max_length=100, do_sample=True)
 
 
 
18
 
19
+ return answer
 
20
 
21
+ # Создаем интерфейс gradio
22
+ gr.Interface(generate_answer, inputs=[gr.Image(), gr.Text()], outputs=gr.Text(), title="Решение задач по фото")